1

This is my code: I am trying to make a function that allows a user to return to the menu once they have finished with the program. However, I keep getting the 'NoSuchElement' error whenever I try to run the code.


import java.io.FileNotFoundException;
import java.util.Scanner;

public class homePage 
{
    public static void main(String[] args) throws FileNotFoundException
    {   
        Scanner sc = new Scanner (System.in);
        Menu.menu();
    
        System.out.println("Would you like to  1:Exit the program or 2: complete another action ");
        int newChoice = sc.nextInt();
    
        System.out.println(newChoice);
    
        switch(newChoice)
        {
        case 1:
            Exit exitScreen = new Exit();
            exitScreen.exit();
            break;
        case 2:
            Menu.menu();
        default:
            System.out.println(newChoice + "invalid input");
            System.out.println("\n");
            main(args);
        }
    }
}

The output:

Would you like to  1:Exit the program or 2: complete another action 
Exception in thread "main" java.util.NoSuchElementException
    at java.base/java.util.Scanner.throwFor(Scanner.java:937)
    at java.base/java.util.Scanner.next(Scanner.java:1594)
    at java.base/java.util.Scanner.nextInt(Scanner.java:2258)
    at java.base/java.util.Scanner.nextInt(Scanner.java:2212)
    at homePage.main(homePage.java:12)

Here is a Minimal, Reproducible Example This refers to the menu class. When the user selects one of the options it is print out the word (display/create/delete) then it should ask the user if they are finished. If they are finished, they will be shown an exit message and if they aren't finished the menu should be displayed again.

import java.io.FileNotFoundException;
import java.util.Scanner;

public class Menu 
{
    public static void menu() throws FileNotFoundException
    {
        Scanner in  = new Scanner(System.in);
        
        System.out.println("Menu \n1 - Display \n2 - Create \n3 - Delete");
        int choice = in.nextInt();
        System.out.println(choice);
        
        switch (choice)
        {
            case 1:
                System.out.println("Display");
            case 2:
                System.out.println("Create");
            case 3:
                System.out.println("delete");
                break;
            default:
                System.out.println("Incorrect Value. Enter a value between 1 and 3");
                break;
        }
        
        in.close();
    }

}

Sanju Siva
  • 11
  • 3
  • 1
    I suspect you're trying to run the code in an environment which doesn't have a console connected for input. – Jon Skeet Jun 01 '21 at 16:20
  • I am running this in Eclipse and have been using the scanner in multiple classes as part of this program I am trying to make. – Sanju Siva Jun 01 '21 at 16:22
  • And does the `Menu` class that you haven't shown us do anything with `System.in`? If you could provide a [mcve] that would make it easier to help you. – Jon Skeet Jun 01 '21 at 16:28
  • (I suspect that recursing like this is also not a great idea, creating a new `Scanner` for the same input... a loop would be more appropriate IMO.) – Jon Skeet Jun 01 '21 at 16:29
  • The `Menu` class also uses a scanner to let them choose with part of the program they would like to navigate to. would it be helpful if I add the code to the `Menu` class as well to the question? Thank you so much for helping! – Sanju Siva Jun 01 '21 at 16:33
  • Well, a [mcve] of some kind would help. You may not need the whole of your current `Menu` class, but take a copy of what you've got at the moment, and remove as much as you can while still being able to reproduce the problem. But fundamentally creating multiple scanners for the same input is likely to be the issue here. – Jon Skeet Jun 01 '21 at 16:37
  • I have added a little snippet of the many class. When these are run together I am receiving the NoSuchElement error. – Sanju Siva Jun 01 '21 at 16:51
  • Right, so your `Menu` class is closing `System.in`, but then you're trying to read from it afterwards (via the original Scanner in `main`). Can you see why that's a problem? You *may* be able to just get away with removing the `in.close()` call, but I think it would be better to create a *single* `Scanner` and pass that to the `Menu.menu()` method (which should then *not* close it). Ensure you only ever create a single `Scanner` around `System.in`, and never close it (as you don't need to). – Jon Skeet Jun 01 '21 at 17:00
  • oh okay, yeah I understand. Should i try using the same scanner the I do in the first class and not opring a new a one in the `menu` class? – Sanju Siva Jun 01 '21 at 17:03
  • Yes, exactly - that's what I was suggesting. – Jon Skeet Jun 01 '21 at 17:03
  • Thank you @JonSkeet ! That worked for me – Sanju Siva Jun 01 '21 at 17:12

0 Answers0