TaylorZ8
TaylorZ8

Reputation: 1

How can I use Java Scanner to take an integer input? NoSuchElementException

So I've been looking for a while to find out how to use scanner to take an input from the user. What happens is I enter an infinite loop after the first scanner. I've tried changing the name of the second scanner to read instead of in, that didn't change anything. I placed in.nextLine() at the end of the of the first scanner instance before I closed to try to solve the problem, but that hasen't seemed to work. Not quite sure why it's happening.

`   private int compPlayInput(){
        int comps = -1; // Initialize 

        Scanner in = new Scanner(System.in); // Start Scanner

        //While the user input isn't between 0 and 2
        while ((comps<0) || (comps>2)){
            System.out.println("Turn: " + turnCount);
            System.out.print("How many computer controlled players will there be: ");
            //did the user input an integer?
            if (in.hasNextInt()){
                comps=in.nextInt();//change comps variable to the user's input
            }
            //The user hasn't entered an integer
            else {
                System.out.println("\n\n** ERROR: Enter an integer x that satisfies 0 <= x <= 2 **\n");
            }
        }
        in.nextLine(); //It seems like this is supposed to fix it, but it doesn't.
        in.close(); // close scanner
        return comps;
    }

    /**
     * Gets a player's guess
     * @return the player's guess (int) between 0 and 4 inclusively
     */
    private int getPlayerGuess(){
        int guess = -1; //initialize
        Scanner in = new Scanner(System.in); //start scanner

        //While the user doesn't input an int between 0 and 4
        while ((guess<0) || (guess>4)){
            System.out.println("Turn: " + turnCount);
            System.out.print("What is your guess: ");
            //If the user input an integer
            if (in.hasNextInt()){
                //make guess = to next int
                guess=in.nextInt();
            }
            else {
                System.out.println("\n\n** ERROR: Enter an integer x that satisfies 0 <= x <= 4 **\n");
            }
        }

        in.close(); // close scanner
        return guess; //return guess
    }`

This is the output in eclipse:

    Turn: 0
How many computer controlled players will there be: 1
Turn: 1
What is your guess: 

** ERROR: Enter an integer x that satisfies 0 <= x <= 4 **

Turn: 1
What is your guess: 

** ERROR: Enter an integer x that satisfies 0 <= x <= 4 **
...

Doesn't allow the user to enter a guess like it does when asking how many computer players there are. I don't know how I should go about fixing this.

I placed the in.next() into the else statement but it seems like I'm getting an error because there isn't anything for the scanner to read. The new output I get is

Turn: 0 How many computer controlled players will there be: 1 Turn: 1 What is your guess:

** ERROR: Enter an integer x that satisfies 0 <= x <= 4 **

Exception in thread "main" java.util.NoSuchElementException at java.util.Scanner.throwFor(Unknown Source) at java.util.Scanner.next(Unknown Source) at HW2TaylorZacharyGame.getPlayerGuess(HW2TaylorZacharyGame.java:114) at HW2TaylorZacharyGame.turn(HW2TaylorZacharyGame.java:52) at HW2TaylorZachary.main(HW2TaylorZachary.java:15)

Upvotes: 0

Views: 181

Answers (3)

TaylorZ8
TaylorZ8

Reputation: 1

I figured out my problem. It seems as though scanner doesn't like to be called twice in a single class file, so my solution was to simply make a new Scanner object where I would put my private variables for the object. So something like this:

public class HW2TaylorZacharyGame {

    private HW2TaylorZacharyPlayer player1 = new HW2TaylorZacharyPlayer();
    private HW2TaylorZacharyPlayer player2 = new HW2TaylorZacharyPlayer();
    private int winner = 0;
    private int turnCount = 0;
    Scanner in = new Scanner(System.in); // Start Scanner

Really simple solution for a problem that gave me a lot of heart-ache. Note, though, I also put in.nextLine() after taking any input from the user. Those two things, for anyone else with the same problem, are the best ways in my opinion to use Scanner. I also never closed Scanner though, so It's a bit of a drawback unfortunately.

Upvotes: 0

Adam
Adam

Reputation: 85

try just in.next() instead of in.nextLine(); and put it inside your else statement

Upvotes: 0

Elliott Frisch
Elliott Frisch

Reputation: 201409

You need to consume the non-int in your else. Something like,

else {
   System.out.printf("ERROR: Enter an integer x that satisfies 0 <= x <= 2: %s "
       + "does not qualify%n", input.nextLine());

}

Upvotes: 0

Related Questions