Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/323.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ssl/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 扫描仪使用nextInt()和循环不断跳过输入whist_Java_Loops_Java.util.scanner_Next - Fatal编程技术网

Java 扫描仪使用nextInt()和循环不断跳过输入whist

Java 扫描仪使用nextInt()和循环不断跳过输入whist,java,loops,java.util.scanner,next,Java,Loops,Java.util.scanner,Next,我使用while循环来确保输入到scanner对象的值是一个整数,如下所示: while (!capacityCheck) { try { System.out.println("Capacity"); capacity = scan.nextInt(); capacityCheck = true; } catch (InputMismatchException e) {

我使用while循环来确保输入到scanner对象的值是一个整数,如下所示:

while (!capacityCheck) {
        try {
            System.out.println("Capacity");
            capacity = scan.nextInt();
            capacityCheck = true;
        } catch (InputMismatchException e) {
            System.out.println("Capacity must be an integer");
        }
    }

但是,如果用户没有输入整数,当它应该返回并接受另一个输入时,它只是重复打印容量,然后在catch中输出,而不要求更多的输入。如何停止此操作?

尝试将此操作放在循环的末尾-

scan.nextLine();
或者最好把它放在挡块里

    while (!capacityCheck) {
        try {
            System.out.println("Capacity");
            capacity = scan.nextInt();
            capacityCheck = true;
        } catch (InputMismatchException e) {
            System.out.println("Capacity must be an integer");
            scan.nextLine();
        }
    }
将这段代码放在catch块中,以使用非整数字符和保留在缓冲区中的新行字符,因此,在输入错误的情况下,无限打印catch sysout

当然,还有其他更干净的方法来实现您想要的,但我想这需要对代码进行一些重构。

使用以下方法:

while (!capacityCheck) {
        System.out.println("Capacity");
        String input = scan.nextLine();
        try {
            capacity = Integer.parseInt(input );
            capacityCheck = true;
        } catch (NumberFormatException e) {
            System.out.println("Capacity must be an integer");
        }
    }
试试这个:

while (!capacityCheck) {
    try {
        System.out.println("Capacity");
        capacity = scan.nextInt();
        capacityCheck = true;
    } catch (InputMismatchException e) {
        System.out.println("Capacity must be an integer");
        scan.nextLine();
    }
}

我认为不需要try/catch或capacityCheck,因为我们可以访问方法hasNextInt,该方法检查下一个标记是否为int。例如,这应该满足您的要求:

    while (!scan.hasNextInt()) { //as long as the next is not a int - say you need to input an int and move forward to the next token.
        System.out.println("Capacity must be an integer");
        scan.next();
    }
    capacity = scan.nextInt(); //scan.hasNextInt() returned true in the while-clause so this will be valid.

它需要使用换行符。为什么要放scan.nextLine;?scan.nextInt将只扫描下一个整数,但当它不是正确的输入时,输入的无效字符将停留在那里,而不会被消耗,从而使while进入无限循环。更新我的答案!:
    while (!scan.hasNextInt()) { //as long as the next is not a int - say you need to input an int and move forward to the next token.
        System.out.println("Capacity must be an integer");
        scan.next();
    }
    capacity = scan.nextInt(); //scan.hasNextInt() returned true in the while-clause so this will be valid.