1

I am pretty new to Java, but know some of C and Python, hence some of Java looks alike. I have a program that worked well, until I tried to create a "main menu". I did as I've always done in the previous languages, but the program will only work ONE time through the loop, then crash.

Code in which error occurs:

while (true)
        {   
            java.util.Scanner in = new java.util.Scanner(System.in);

            System.out.println("Alternative 1. Add A New Person To Database");
            System.out.println("Alternative 2. Quit The Program");

            int choice = in.nextInt(); //This is where error is found! (:22)

            if (choice==1)
            {
                choice1();
            }
            if (choice==2)
            {
                System.out.println("Look at the file text.txt");
                System.exit(-1);
            }
        }

Error Message:

Exception in thread "main" java.util.NoSuchElementException
    at java.util.Scanner.throwFor(Unknown Source)
    at java.util.Scanner.next(Unknown Source)
    at java.util.Scanner.nextInt(Unknown Source)
    at java.util.Scanner.nextInt(Unknown Source)
    at EgnaProgrammet.main(EgnaProgrammet.java:22)

I'm certain that something is wrong with the input since the problem occurs the second time I want to make a input in the while. Could it be because choice already has a value?

Appriciate any help!

Diaco
  • 216
  • 1
  • 3
  • 14
  • My input is either 1 or 2. ONLY those. Observe that it works fine the first "loop". Problem occurs the second time. – Diaco Nov 04 '15 at 07:44
  • You should use the hasNextXXXX() methods from the Scanner class to make sure that there is an integer ready to be read. Please check this link http://stackoverflow.com/questions/12832006/scanner-error-with-nextint – Bhushan Nov 04 '15 at 07:45
  • 2
    In addition you should only use one instance of a scanner instead of creating a new one at each iteration. – SomeJavaGuy Nov 04 '15 at 07:48
  • @KevinEsche Your comment is the root cause of the problem. But yes, he should also maybe have some logic to handle a non integer input. – Tim Biegeleisen Nov 04 '15 at 07:49
  • will check the links and try to solve it. as for input handling, I will do it when this problem is solved. One step at a time! :) – Diaco Nov 04 '15 at 07:55
  • @BhushanPatil if I do that, then the answer "choice", will be inside the if-statement and therefore will not be seen by the rest of the method. – Diaco Nov 04 '15 at 08:20
  • What output do you get from `System.in.available()`? – Tim Biegeleisen Nov 04 '15 at 08:48
  • 0. not sure what that does anyway – Diaco Nov 04 '15 at 09:02
  • It will tell you whether the `System.in` stream is available? If this is returning false, then you likely have found the problem. – Tim Biegeleisen Nov 04 '15 at 09:27

2 Answers2

0

This question is a duplicate of this SO post (and likely a few others). What is happening is that in the first iteration of the while loop you are instantiating a Scanner object using the System.in input stream. In each subsequent iteration of the loop, you are creating a new Scanner object, which then closes the System.in input stream.

Change your code to this:

// declare your Scanner only once, *outside* the while loop
java.util.Scanner in = new java.util.Scanner(System.in);
System.out.println(System.in.available());

while (true) {
    System.out.println("Alternative 1. Add A New Person To Database");
    System.out.println("Alternative 2. Quit The Program");

    int choice = in.nextInt(); //This is where error is found! (:22)

    if (choice == 1) {
        choice1();

    } else if (choice == 2) {
        System.out.println("Look at the file text.txt");
        System.exit(-1);
    }
}
Community
  • 1
  • 1
Tim Biegeleisen
  • 387,723
  • 20
  • 200
  • 263
  • I had this previously, but when encountering the problem, decided to put it inside and also added "in.close();" at the bottom. But I realised that it wasn't the problem, so probably should've brought it outside again. – Diaco Nov 04 '15 at 07:54
  • I think it is the problem. What error message do you get when running my code? – Tim Biegeleisen Nov 04 '15 at 07:57
  • It makes no difference to put it outside (except I dont waste resources calling it every time), hence I get the same error. Problem seems to be as described by the others. will try to do that now – Diaco Nov 04 '15 at 07:59
  • Try adding this line of code immediately after you create the `Scanner`: `System.out.println(System.in.available());` – Tim Biegeleisen Nov 04 '15 at 08:01
0

You must have next line for scanner. I think this post will help you

Scanner is skipping nextLine() after using next(), nextInt() or other nextFoo() methods

also scanner has a method "hasNext()" you can use it like:

while(scanner.hasNext()){
// do sth
}
Community
  • 1
  • 1
bmavus
  • 872
  • 1
  • 6
  • 19