The Artima Developer Community
Sponsored Link

Java Answers Forum
Please help with string problem

1 reply on 1 page. Most recent reply: Jul 28, 2003 4:52 AM by David

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 1 reply on 1 page
Philip Pompili

Posts: 12
Nickname: ppompili
Registered: Jul, 2003

Please help with string problem Posted: Jul 27, 2003 10:58 PM
Reply to this message Reply
Advertisement
I am having a problem with a program for school.. I have done the majority of t but the problem is that I cant get my substring to work and I dont know how to get the stars into a string(you will see in the program what I am talking about) here is my program .. please help...

import york.*;
/*
This class plays a game of Hang Man
*/

public class HangManGame
{
public static void main (String[] args)
{
York.println ("Please enter a secret word.");
String secretWord = York.readLine();
char guess;
String answer;
int i = secretWord.length();
int l;
//initializes all variables
York.println("Please choose a letter");
for (l=0; l<i ;l++)
{
York.print(" * ");
}
//prints out astreiks according to how many leters are in secretWord
while (i!=0)
{
//counts the number of guesses
York.println();
guess = York.readChar();
for (int j=0; j<secretWord.length(); j++ )
{
if (guess==secretWord.charAt(j))
{
York.println();
York.println("You guessed "+guess+ ".");
York.println("You have " +(i)+ " guesses left");
answer = line.substring(0,j)+guess+line.substring(j+1);
York.println(answer);
}
}
York.println("Please guess the next letter");
}
}
}


David

Posts: 18
Nickname: malven
Registered: Jul, 2003

Re: Please help with string problem Posted: Jul 28, 2003 4:52 AM
Reply to this message Reply
In Java, unlike some other programming languages, Strings are immutable. Once you construct a String there is no way to change the characters within it. Look at java.lang.StringBuffer instead, or simply use a character array.

I saw a few other problems in your code you need to work at:

The while loop checks the value of the variable "i", which is set to the string length and never changes. So this loops indefinitely. To loop only until all letters are guessed, you need to use a variable that is incremented or decremented whenever a correct guess is made. So looks like you need a new variable that represents the number of letters correctly guessed.

BTW, you might want to rename some of these variables to make things easier to read. For example, make the length of the secret word variable called "length" or "wordLength" instead of "i", which is typically only used as a loop index variable. And I would avoid a single letter 'l' as a variable, as it easily gets confused with the number '1'.

Be careful with the substring() method on String. The initial index you give it has to be "within" the string bounds. In your code, line.substring(j+1) will cause an exception when j == secretWord.length-1, which will happen at in the for loop if no letters are matched by the guess.

Hope this helps!

Flat View: This topic has 1 reply on 1 page
Topic: Yahoo Games Help Previous Topic   Next Topic Topic: Problem with changing of BufferSize (audio streaming/ JMF)

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use