Java:使用Scanner in.hasNextInt()的无限循环

Tom*_*mek 15 java loops infinite java.util.scanner

我使用以下代码:

while (invalidInput)
{
    // ask the user to specify a number to update the times by
    System.out.print("Specify an integer between 0 and 5: ");

    if (in.hasNextInt())
    {
        // get the update value
        updateValue = in.nextInt();

        // check to see if it was within range
        if (updateValue >= 0 && updateValue <= 5) 
        { 
            invalidInput = false; 
        } 
        else 
        {
            System.out.println("You have not entered a number between 0 and 5. Try again.");
        }
    } else
    {
        System.out.println("You have entered an invalid input. Try again.");
    }
}
Run Code Online (Sandbox Code Playgroud)

但是,如果我输入'w',它会告诉我"你输入了无效的输入.再试一次." 然后它将进入一个无限循环,显示文本"指定0到5之间的整数:您输入的输入无效.请再试一次."

为什么会这样?该程序是否应该等待用户输入并在每次到达语句时按Enter键:

if (in.hasNextInt())
Run Code Online (Sandbox Code Playgroud)

Kal*_*see 18

在最后一个else块中,您需要清除扫描仪中的"w"或其他无效输入.您可以通过调用next()扫描程序并忽略其返回值来丢弃该无效输入来执行此操作,如下所示:

else
{
      System.out.println("You have entered an invalid input. Try again.");
      in.next();
}
Run Code Online (Sandbox Code Playgroud)


pol*_*nts 10

问题是你没有推进Scanner过去有问题的输入.来自hasNextInt()文档:

返回true此扫描器输入中的下一个标记是否可以int使用该nextInt()方法解释为默认基数中的值.扫描仪不会超过任何输入.

所有hasNextXXX()方法都是如此:它们返回true或者false没有推进Scanner.

这是一个说明问题的片段:

    String input = "1 2 3 oops 4 5 6";
    Scanner sc = new Scanner(input);
    while (sc.hasNext()) {
        if (sc.hasNextInt()) {
            int num = sc.nextInt();
            System.out.println("Got " + num);
        } else {
            System.out.println("int, please!");
            //sc.next(); // uncomment to fix!
        }
    }
Run Code Online (Sandbox Code Playgroud)

你会发现这个程序将进入一个无限循环,int, please!反复询问.

如果取消注释该sc.next()语句,那么它将Scanner通过失败的令牌hasNextInt().然后该程序将打印:

Got 1
Got 2
Got 3
int, please!
Got 4
Got 5
Got 6
Run Code Online (Sandbox Code Playgroud)

失败hasNextXXX()检查不会跳过输入这一事实是故意的:它允许您在必要时对该令牌执行其他检查.这是一个例子来说明:

    String input = " 1 true foo 2 false bar 3 ";
    Scanner sc = new Scanner(input);
    while (sc.hasNext()) {
        if (sc.hasNextInt()) {
            System.out.println("(int) " + sc.nextInt());
        } else if (sc.hasNextBoolean()) {
            System.out.println("(boolean) " + sc.nextBoolean());
        } else {
            System.out.println(sc.next());
        }
    }
Run Code Online (Sandbox Code Playgroud)

如果您运行此程序,它将输出以下内容:

(int) 1
(boolean) true
foo
(int) 2
(boolean) false
bar
(int) 3
Run Code Online (Sandbox Code Playgroud)