How Do I Safely Scan for Integer Input? [duplicate]
问题 This question already has answers here : How to use Scanner to accept only valid int as input (6 answers) How to handle infinite loop caused by invalid input (InputMismatchException) using Scanner (5 answers) Closed last year . Scanner scanner = new Scanner(); int number = 1; do { try { option = scanner.nextInt(); } catch (InputMismatchException exception) { System.out.println("Integers only, please."); } } while (number != 0); Despite the exception handling, this code will enter an infinite