Hello, the following question arose: a number is entered from the keyboard if it is an integer, then some code will be executed, if not, a line is output that the number is not an integer and a new one is entered. There is a code:

public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.print("Введите кол-во интервалов: "); if (!sc.hasNextInt()) { while (sc.hasNextInt() == false){System.out.println("Некорректный тип введеного числа! Повторите попытку: ");} } else { int n = sc.nextInt(); } } } 

If I enter a whole everything is normal, when entering for example 2.3, an infinite loop works and I cannot enter a number. The question is trivial, but tell me, please, what is wrong.

  • I know that the question is stupid, but nevertheless it arose - Muscled Boy

4 answers 4

I would use recursion, below is an example.

 public class Main { public static void main(String[] args) { myMain(); } private static void myMain() { Scanner sc = new Scanner(System.in); System.out.print("Введите кол-во интервалов: "); if (!sc.hasNextInt()) { System.out.println("Повторите попытку!"); myMain(); } else { int n = sc.nextInt(); } } } 

wrote another version without recursion, here you are

 public class Main { public static void main(String[] args) { Scanner sc; System.out.print("Введите кол-во интервалов: "); while (true) { sc = new Scanner(System.in); if (!sc.hasNextInt()) { System.out.print("Некорректный тип введеного числа! Повторите попытку: "); } else { int n = sc.nextInt(); sc.close(); break; } } } } 
  • 3
    Not a very good solution. With a multitude of incorrect attempts, a large number of unnecessary objects will be created. - pavlofff
  • one
    @pavlofff, I do not argue, but for educational purposes it will come down) and yes, thanks for koment)) - diofloyk
  • one
    @MuscledBoy aha, that's right. If sc is not an int, then just myMain () is called again and so on until you enter an int value, well, or as pavlofff correctly noted, until you have run out of memory allocated for VM - diofloyk
  • one
    The @MuscledBoy while loop condition (true) can be treated as an infinite loop. With the closure of the stream, I think in this case, you can not zamarachivatsya) correct someone if I'm wrong - diofloyk
  • one
    @Diofloyk, thanks again - Muscled Boy
 public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.print("Введите кол-во интервалов: "); while (!scanner.hasNextInt()) { System.out.println("Некорректный тип введеного числа: " + scanner.next()); } System.out.println("Введенное целое число: " + scanner.nextInt()); } 

The scanner.next() call reads the next token, due to which the next scanner.hasNextInt() call will be applied to the token following the one just read (by means of scanner.next() ), thereby preventing an infinite loop.


Well, or a more general solution (in the previous example, at the end of the tokens in the console, you will expect to enter a new one without notifying it):

 public static void main(String[] args) { Scanner consoleScanner = new Scanner(System.in); while (true) { System.out.print("Введите кол-во интервалов: "); Scanner lineScanner = new Scanner(consoleScanner.nextLine()); while (lineScanner.hasNext() && !lineScanner.hasNextInt()) System.out.println("Некорректный тип введеного числа: " + lineScanner.next()); if (lineScanner.hasNext() && lineScanner.hasNextInt()) { System.out.println("Введенное целое число: " + lineScanner.nextInt()); break; } } } 
  • @StateltPrimitive, thanks, I'll figure it out - Muscled Boy 4:14 pm
 public class Main { public static void main(String[] args) { int i; boolean isInt = false; Scanner sc; do { System.out.print("Введите целое число: "); sc = new Scanner(System.in); if (sc.hasNextInt()) { i = sc.nextInt(); isInt = true; System.out.println(i); } else { System.out.println("Некорректный тип введеного числа! Повторите попытку: "); } } while (!isInt); sc.close(); } } 
  • I had a similar option, but in this case it will not be possible to enter a new number - Muscled Boy
  • thanks for your reply - Muscled Boy
  • and what the close () method does; ? - Muscled Boy
  • @MuscledBoy closes the input stream so that it does not consume resources when it is no longer required. - pavlofff
  • thanks, so clearer - Muscled Boy

if you start checking whether the number is of type int, "if (! sc.hasNextInt ())", and then why you create a while loop where the condition is the same as in "if (! sc.hasNextInt ())". and sc.hasNextInt will always be false. therefore, an endless loop.