David
Posts: 18
Nickname: malven
Registered: Jul, 2003
|
|
Re: Please help with string problem
|
Posted: Jul 28, 2003 4:52 AM
|
|
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!
|
|