David
Posts: 18
Nickname: malven
Registered: Jul, 2003
|
|
Re: Problems with my code
|
Posted: Jul 28, 2003 4:11 PM
|
|
What the compiler is telling you is absolutely right. You have a declaration for answer that looks like this:
String answer;
and then a subsequent assignment that looks like this:
answer = answer.substring(0,j)+guess+answer.substring(j+1);
On the right hand side of the equals sign, the first time through the loop you will be using a reference that never got initialized to a value.
You need to initialize the variable before using it. To do this, think more clearly about that the answer variable is meant to represent. At the beginning, before the user does anything, what should its value be? When they guess a letter correctly, what should it look like then? Try to work out on paper how this variable should change, and then you can see how to modify the code.
Two other quick notes:
1. You have code "while (i!=0 && answer!=secretWord)". This isn't going to work. You need to compare the *contents* of the strings, not the identity (references). Look at the equals() method of String to understand the difference.
2. There are still problems with the overall logic and use of substring() that I answered in the other thread you posted with this code.
|
|