0

I've been reading about issues with Scanner, but even after adding in an extra in.nextLine() or in.nextInt() all Java does is move the NoSuchElementException up to that line. Any suggestions? I'm trying to create an auto-looping menu selection for my program. Every time it loops case 1, it has an issue.

while (!done) {
 System.out.println("Welcome, please type a number for selecting from the following: \n 1. Insert Process \n 2. Print out a list of processes \n 3. See and remove first priority process \n 4. Quit");

 Scanner in = new Scanner(System.in);
 int selector = 0;
 print(); in .nextLine();
 selector = in .nextInt();

 switch (selector) {
  case 1:
   System.out.println("Please enter a priority for the new process > 0");
   Scanner pin = new Scanner(System.in);
   priority = pin.nextInt();
   if (priority > 0) {
    maxHeapInsert(priority); //allows user to set priority
    in .close(); in = new Scanner(System.in);
    break;

   } else {
    System.out.println("ERROR, you did not enter a number greater than 0");
   }
   break;
 }
}
Dinith Rukshan Kumara
  • 638
  • 2
  • 10
  • 19
Ian S.
  • 1

3 Answers3

0

Use scanner's hasNextLine and it's other variants before trying to get the token with next*.

There is no necessity to use multiple scanners for System.in.

sn42
  • 2,353
  • 1
  • 15
  • 27
0

Try below code.

Note that there is no need to use/declare multiple scanners. Also try to avoid declaring variables inside loops as it is not efficient(memory/performance).

boolean done = false;
    int priority = 0, selector = 0;
    try(Scanner in=new Scanner(System.in))
    {
        while (!done ) {
            System.out.println("Welcome, please type a number for selecting from the following: \n 1. Insert Process \n 2. Print out a list of processes \n 3. See and remove first priority process \n 4. Quit");
            selector = in.nextInt();
            in.nextLine();

            switch(selector){
            case 1:
                System.out.println("Please enter a priority for the new process > 0");
                priority = in.nextInt();
                in.nextLine();

                if (priority > 0) {
                    maxHeapInsert(priority);//allows user to set priority
                    break;
                } else {
                    System.out.println("ERROR, you did not enter a number greater than 0");
                }
                break;
            }
        }
    }
LenosV
  • 172
  • 1
  • 6
  • 1
    Note that the try with resource statement will also close `System.in` and not just the scanner. If you want to use `System.in` outside the try block, either don't use try with resource or [shield it](https://stackoverflow.com/a/14143048/8089107) – sn42 Mar 30 '18 at 14:03
  • Unfortunately I'm still getting that same error at selector = in.nextInt(); – Ian S. Mar 30 '18 at 18:07
0

Try the code below:

while (!done) {
        System.out.println(
                "Welcome, please type a number for selecting from the following: \n 1. Insert Process \n 2. Print out a list of processes \n 3. See and remove first priority process \n 4. Quit");

        Scanner in = new Scanner(System.in);
        int selector = 0;
        selector = in.nextInt();

        switch (selector) {
        case 1:
            System.out.println("Please enter a priority for the new process > 0");
            Scanner pin = new Scanner(System.in);
            priority = pin.nextInt();
            if (priority > 0) {
                maxHeapInsert(priority); //allows user to set priority
                break;
            } else {
                System.out.println("ERROR, you did not enter a number greater than 0");
            }
            break;
        }
    }

The reason is that when you use in.close() it will also close the System.in so you will read -1 and throw the exception. Remember a program cannot reopen the System.in after closing it.

YCXU1993
  • 142
  • 3