Issue
I am a beginner Java programmer and using Netbeans IDE.
Here is my code:
Scanner user_input = new Scanner(System.in);
String name;
System.out.println("Hello, what is your name?");
name = user_input.next();
System.out.println("");
String name_answer;
System.out.println("Your name is " + name + ". Is this correct? (yes/no)");
name_answer = user_input.next();
System.out.println("");
if (name_answer.equals("yes")) {
System.out.println("Thank you, " + name + ". Please proceed to the next question.");
} else if (name_answer.equals("no")) {
System.out.println("Please reinput your name correctly.");
for (name_answer.equals("no")) {
String name_again;
System.out.println("");
System.out.println("What is your correct name?");
name_again = user_input.next();
System.out.println("");
System.out.println("Your name is " + name_again + ". Is this correct? (yes/no)");
name_answer = user_input.next();
}
}
At the line of the for loop [ for(name_answer.equals("no")) ], it is giving me this problem:
';' expected
Should check the method return value
I can't find any solutions, please help!
Also, I am trying to create the loop that whenever the user answers "no" to the question "Is this correct (yes/no)", it loops to the "What is your correct name?". Is this how the loop will turn out, if not, how can i fix it?
Thank you for the help.
Solution
For loop takes 3 parameters: The syntax of a for loop is −
for(initialization; Boolean_expression; update) {
// Statements
}
for (name_answer.equals("no"))
Change it to:
for (;name_answer.equals("no");)
But as other people suggested, using while
is a better alternative.
Answered By - vikiiii