3

I can´t get my reloadable method "menu" to work and it looks like it has something to do with scanner.

I´m sure it´s an easy solution but I just can´t find it

 public class menuControl
{

    public static void main(String[] args)
{
        switch (menu())
        {
        case 1: System.out.println(1); menu();break;
        case 2: System.out.println(2); menu();break;
        case 3: System.out.println(3); menu();break;
        case 4: System.out.println(4); menu();break;
        case 0: System.out.println(0); menu();break;
        }
}
    public static int menu()
    {
        Scanner in = new Scanner(System.in);
        System.out.println("Choice 1");
        System.out.println("Choice 2");
        System.out.println("Choice 3");
        System.out.println("Choice 4");
        System.out.print("Choose: ");
        int choice = in.nextInt();
        System.out.println();
        in.close(); 
        if (choice > 0 && choice < 5)
        {
            return choice;
        }
        else
        {
            System.out.println("Wrong choice!");
            return 0;
        }
    }
}

I get this 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 Cashmachine.menu(Cashmachine.java:47) //The line"int choice = in.nextInt();"
    at Cashmachine.main(Cashmachine.java:23) //The line "case 1: System.out.println(1); menu();break;"
Robert
  • 33
  • 4
  • Possible duplicate [Exception in thread “main” java.util.NoSuchElementException: No line found - Using scanner input](http://stackoverflow.com/questions/19828015/exception-in-thread-main-java-util-nosuchelementexception-no-line-found-usi). – MChaker May 10 '15 at 23:48
  • do you really want bad input to default to zero? You could give users another chance – D. Ben Knoble May 11 '15 at 00:30
  • Yes, if the user makes a bad input it will jump back to menu choice so he will get another chance. As per the suggestion with the for statement. I had tried a different approach that didn´t work. – Robert May 11 '15 at 01:13

1 Answers1

1

Remove the in.close(). Your Scanner in wraps the global System.in, once you close() it you will not be able to use nextInt().

You could extract the Scanner to a field (or an argument) like

public static int menu(Scanner in) {
    // Scanner in = new Scanner(System.in);
    // ...
    // in.close();
    // ...

And I think you wanted a loop in main() like

Scanner in = new Scanner(System.in);
for (;;) {
    switch (menu(in)) {
    case 1:
        System.out.println(1);
        break;
    case 2:
        System.out.println(2);
        break;
    case 3:
        System.out.println(3);
        break;
    case 4:
        System.out.println(4);
        break;
    case 0:
        System.out.println(0);
        break;
    }
}
Elliott Frisch
  • 183,598
  • 16
  • 131
  • 226
  • 1
    It might be better to declare and initialize the scanner in a field, so that a new one doesn't need to be created each time you all the function. – Vincent May 10 '15 at 23:48