-1

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)

N00b Pr0grammer
  • 3,930
  • 4
  • 28
  • 40
Pope Sua
  • 1
  • 2
  • The exception is java.util.NoSuchElementException – Pope Sua Aug 17 '16 at 18:03
  • Please [edit] your question to include the full stack trace of any errors you get. – azurefrog Aug 17 '16 at 18:04
  • Possible duplicate of [NoSuchElementException with Java.Util.Scanner](http://stackoverflow.com/questions/13729294/nosuchelementexception-with-java-util-scanner) – David Aug 17 '16 at 18:05

2 Answers2

0

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;
Imesha Sudasingha
  • 2,885
  • 1
  • 18
  • 29
0

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.

11thdimension
  • 9,084
  • 1
  • 20
  • 60