Reputation: 1
The code below is supposed to ask the user to enter start into the console and then if the user hits start "Let's begin" if not, "Wrong word entered" is printed and the method loops itself.
do {
System.out.println("Type start to play");
String word = lineReader.nextLine();
String word2 = ("start");
boolean valid;
valid =(word.equals (word2));
if (valid){
System.out.println("Let's begin");
} else {
System.out.println("Wrong Word Entered");
}
} while (!valid);
Upvotes: 0
Views: 57
Reputation: 140494
The problem is that you declare valid
inside the loop, so you can't use it in the while (!valid)
condition.
The variable is only visible to inside the do/while
loop if you declare it there (this is called the variable's "scope").
Move boolean valid
before the do
:
boolean valid;
do {
// Code which assigns true or false to valid.
} while (!valid);
Upvotes: 4