I am writing a program that asks the user to input his name, address and phone number. When the data is entered the program shall print the data and ask the user to verify the data by entering YES or NO. This process shall be repeated until the user is satisfied and answers YES to the question.
Now in my case I may have put a if-else statement inside the while loop in an inappropriate way. That's why it's not working as it is expected to be. How can I solve this?
Also I only have tried the promt asking to enter the users name. But if I want to add more prompt with different question in the similiar way then how could I do that?
Code:
package userinfo;
import java.util.Scanner;
public class UserInfo {
public static void main(String[] args) {
String name;
String yes = "YES";
String no = "NO";
Scanner userInput = new Scanner(System.in);
System.out.println("Enter your name:");
name = userInput.next();
System.out.println("Please varify your name by typing YES or NO");
while (true) {
String input = userInput.next();
if (input == yes) {
System.out.println("Your name is: " + name);
}
if (input == no) {
System.out.println("Enter your name again");
} else {
System.out.println("Invalid input! Enter value again:");
break;
}
}
}
}