Java scanner input validation [duplicate]
问题 This question already has answers here : Validating input using java.util.Scanner [duplicate] (6 answers) Closed 3 years ago . I am trying to accept only three numbers from my input: 1, 2, 3. Any other different than that must be invalid. I have created method but I don't have an idea why it did not working. What must I change? int number; do { System.out.println("Enter 1, 2 or 3"); while (!scanner.hasNextInt()) { System.out.println("Invalid input!"); } number = scanner.nextInt(); } while