What is wrong with my scanner code?

Can anybody tell me what is wrong with my code as shown below? The first case works perfectly fine but the second and third case throws an exception :

Does this have something to do with the while loops at the start of case 2 and 3?

public static void main(String[] args) {

    Scanner scan = new Scanner(System.in);
    int option = scan.nextInt();

    switch (option) {
        case 1: int line = scan.nextInt();
                for (int i = 0; i < line; i++){

                    String operator = scan.next();

                    int n1 = scan.nextInt();
                    int n2 = scan.nextInt();

                    Boolean no1 = (n1 == 1) ? true:false;
                    Boolean no2 = (n2 == 1) ? true:false;

                    if (operator.equals("AND")) {
                        int result = (no1 && no2) ? 1:0;
                        System.out.println(result);
                    } else {
                        int result = (no1 || no2) ? 1:0;
                        System.out.println(result);
                    }

                }
                break;
        case 2: while (!scan.nextLine().equals("0")) {
                    String operator = scan.next();

                    int n1 = scan.nextInt();
                    int n2 = scan.nextInt();

                    Boolean no1 = (n1 == 1) ? true:false;
                    Boolean no2 = (n2 == 1) ? true:false;

                    if (operator.equals("AND")) {
                        int result = (no1 && no2) ? 1:0;
                        System.out.println(result);
                    } else {
                        int result = (no1 || no2) ? 1:0;
                        System.out.println(result);
                    }

                }
                break;
        case 3: while (scan.hasNextLine()) {
                    String operator = scan.next();

                    int n1 = scan.nextInt();
                    int n2 = scan.nextInt();

                    Boolean no1 = (n1 == 1) ? true:false;
                    Boolean no2 = (n2 == 1) ? true:false;

                    if (operator.equals("AND")) {
                        int result = (no1 && no2) ? 1:0;
                        System.out.println(result);
                    } else {
                        int result = (no1 || no2) ? 1:0;
                        System.out.println(result);
                    }
                }
                break;
        default: System.out.println("Error");
                 break;
    }
}

Exception in thread "main" java.util.NoSuchElementException

at java.util.Scanner.throwFor(Scanner.java:862)

at java.util.Scanner.next(Scanner.java:1371)

at HelloWorld.main(HelloWorld.java:64)


Scanner.nextInt() won't consume the new line followed by it.

That's why scan.nextLine() call in the second case would consume n , a single new line character, making the while condition true .

Thus inputted "0" is still not consumed and would go in operator , input meant for the operator would be a string and would not be consumed by scan.nextInt(), this is the error.

To remedy this, call scan.nextLine() after the first scan.nextInt() call.


In case 2:

while (scan.nextLine().equals("0"))

The above line reads the next line. Then the next time you call scan.next() you may not have anything to read. That's why you get java.util.NoSuchElementException exception.

In the while loop, check with scan.hasNext() . Then inside the while loop, read the input and check whether it equals to 0 .

while (!scan.hasNext()) {
    String operator = scan.next();
    if (operator.equals("0")) break;
    int n1 = scan.nextInt();
    int n2 = scan.nextInt();

    boolean no1 = (n1 == 1) ? true:false;
    boolean no2 = (n2 == 1) ? true:false;

   if (operator.equals("AND")) {
           int result = (no1 && no2) ? 1:0;
           System.out.println(result);
   } else {
           int result = (no1 || no2) ? 1:0;
           System.out.println(result);
   }

}
break;
链接地址: http://www.djcxy.com/p/96096.html

上一篇: 关闭与System.in链接的扫描仪

下一篇: 我的扫描仪代码有什么问题?