I'm trying to create a method to validate a user input to be "1". Therefore I used a while loop for validation. Yet, it enters the while loop when the input is "1".
public static int switchInput() {
System.out.print("\n" + "Enter your selection: ");
Scanner userInput = new Scanner(System.in);
String selection = userInput.next();
while (selection != "1" /*&& selection != "2" && selection != "3"*/){
System.out.println("Enter 1: ");
//System.out.println("Please enter either '1','2' or '3': ");
selection = userInput.next();
}
int result = Integer.parseInt(selection);
return result;
}
Output:
What would you like?
1: Regular Hamburger
2: Healthy Burger
3: Deluxe Burger
Enter your selection:
1
Enter 1:
1
Enter 1:
1
Enter 1:
Process finished with exit code -1