Search code examples
javaexceptionjava.util.scannernosuchelementexception

java.util.NoSuchElementException when using a scanner


So after calling a function(in my main) that I will post below, creating a new scanner object in the same main did not work and gave me the following error:

Exception in thread "main" java.util.NoSuchElementException
at java.util.Scanner.throwFor(Scanner.java:862)
at java.util.Scanner.next(Scanner.java:1485)
at java.util.Scanner.nextInt(Scanner.java:2117)
at java.util.Scanner.nextInt(Scanner.java:2076)

linking me to the last line in this code:

    int numHouse = 0;
    int numSeed = 0;
    System.out.println("Enter the total number of houses on the board: ");
    Scanner houseInput = new Scanner(System.in);
    numHouse = houseInput.nextInt();

which is placed right after I call the function (I do close the scanner later on in the above code). When I comment out calling the function, this scanner works, and so something in the function is causing an error in scanner. I've spent hours looking but couldn't resolve it, would appreciate it so much if anyone could. The function:

    public void InitializePlayers() {
    //change numPlayers to variable that contains number of players
    boolean validInput = false;
    do {
        System.out.print("Enter Number of Players (1/2): ");
        Scanner userInput = new Scanner(System.in); 
        int numPlayers = userInput.nextInt();
        System.out.print("\n");
        if(numPlayers == 1) {
            //One Human Player, One AI Player
            System.out.println("Single Player Selected");
            validInput = true;
            System.out.print("\n");             
            System.out.print("Enter Player One Name: ");

            //Initialize Player 1
            String p0_name = userInput.next();
            Player Human1 = new Player(p0_name,0,false);
            this.setPlayer0(Human1);

            //Initialize AI
            String AI_name = "Hal";
            Player AI = new Player(AI_name,1,true);
            this.setPlayer1(AI);
            userInput.close();
        }
        else if(numPlayers == 2){
            //Two Human Players
            System.out.println("Two Players Selected");
            validInput = true;
            System.out.print("Enter Player One Name: ");

            //Initialize Player 1
            String p0_name = userInput.next();
            Player Human1 = new Player(p0_name,0,false);
            this.setPlayer0(Human1);

            //Initialize Player 2
            System.out.print("Enter Player Two Name: ");
            String p1_name = userInput.next();
            Player Human2 = new Player(p1_name,0,false);
            this.setPlayer1(Human2);
            userInput.close();
        }
        else {
            System.out.println("Error, Player 1 Not set!");
            userInput.close();
        }
    }while(!validInput);
}

(I'm always entering 2 when I test it) I've tried removing the do-while loop, but that didn't resolve the issue. Thank you


Solution

  • Better if can use one Scanner to read from System.in for the whole app

    Closing the scanner which takes player will close the InputStream which will cause this NoSuchElmentException.Better if you can define one Scanner and use it in both occasions on taking input