简体   繁体   English

Java Scanner.hasNextInt() 不等待输入

[英]Java Scanner.hasNextInt() does not wait for input

I am trying to read only valid integers from the console.我试图从控制台中只读取有效的整数。 I got the example below from another Stackoverflow answer and it should work.我从另一个 Stackoverflow 答案中得到了下面的示例,它应该可以工作。 If I call the method once it works but on the second call it somehow ignores the sc.hasNextInt() statement and doesn't wait for an input here or thinks there is already an input.如果我在该方法工作后调用该方法,但在第二次调用时,它会以某种方式忽略 sc.hasNextInt() 语句,并且不会在此处等待输入或认为已经有输入。

   Scanner sc = new Scanner(System.in);
    int input;
    
    do {
        System.out.println("Please enter a number [0-200]!");
        while (!sc.hasNextInt()) {
            System.out.println("That's not a number!");
            sc.next();
        }
        input = sc.nextInt();
    } while (input >= 0 && input <= 200);
    
    sc.close();
    return input;

Example of input:输入示例:

Please enter a number [0-200]!
d
That's not a number!
-1
Please enter a number [0-200]!
That's not a number!
Exception in thread "main" java.util.NoSuchElementException
    at java.base/java.util.Scanner.throwFor(Scanner.java:937)
    at java.base/java.util.Scanner.next(Scanner.java:1478)
    at Aufgabe5.getInput(Aufgabe5.java:44)
    at Aufgabe5.main(Aufgabe5.java:112)

nextInt() works in a way that if you input a number and then press Return "The enter key" that key is registered and waiting, when the nextInt() is called again, that registered key will then get consumed by the nextInt() and program will continue without waiting an input, cos it assumed that there was an input. nextInt()的工作方式是,如果您输入一个数字,然后按 Return “回车键”,则该键已注册并等待,当再次调用nextInt()时,该注册键将被nextInt()使用并且程序将继续而不等待输入,因为它假设有输入。 to get around this you can follow the scanner.nextInt() with a scanner.nextLine() to consume that Return key.为了解决这个问题,您可以在scanner.nextInt()之后使用scanner.nextLine()来使用该Return 键。

other way it can be done by saving a line of code is by replacing scanner.nextInt() with Integer.valueOf(scanner.nextLine()) .通过保存一行代码可以完成的另一种方法是将scanner.nextInt()替换为Integer.valueOf(scanner.nextLine()) or better yet, Integer.parseInt(scanner.nextLine())或者更好的是, Integer.parseInt(scanner.nextLine())

If you are looking for code, below method will work for your situation.如果您正在寻找代码,以下方法将适用于您的情况。

public static int getNumber() {

    Scanner sc = new Scanner(System.in);
    int input;

    System.out.println("Please enter a number [0-200]!");

    while (true){
        try {
            input = Integer.parseInt(sc.nextLine());


            if (input >= 0 && input <= 200){
                return input;
            }else {
                System.out.println("Please enter a number [0-200]!");
            }



        } catch (NumberFormatException e) {
            System.out.println("That's not a number!");
            System.out.println("Please enter a number [0-200]!");
        }

    }

}

So apparently removing the sc.close() statement made it work.因此,显然删除 sc.close() 语句使其工作。 Idk why though不知道为什么

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM