如何使用Java Scanner进行整数输入? NoSuchElementException异常

时间:2015-10-21 22:54:52

标签: java java.util.scanner infinite-loop

所以我一直在寻找一段时间来了解如何使用扫描仪从用户那里获取输入。发生的事情是我在第一台扫描仪后进入无限循环。我已经尝试将第二个扫描仪的名称改为读取而不是改为,但没有改变任何东西。在我关闭以尝试解决问题之前,我将.nextLine()放在第一个扫描程序实例的末尾,但这似乎没有用。不太确定为什么会这样。

`   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
    }`

这是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 **
...

当询问有多少台计算机玩家时,不允许用户输入猜测。我不知道应该如何解决这个问题。

我将in.next()放入了else语句中,但似乎我收到了一个错误,因为扫描程序没有任何东西可以读取。我得到的新输出是

  

转弯:0将有多少计算机控制的球员:1转:1   你的猜测是什么:

     

**错误:输入一个满足0&lt; = x&lt; = 4 **的整数x

     

线程“main”中的异常java.util.NoSuchElementException at   java.util.Scanner.throwFor(未知来源)at   java.util.Scanner.next(未知来源)at   HW2TaylorZacharyGame.getPlayerGuess(HW2TaylorZacharyGame.java:114)at at   HW2TaylorZacharyGame.turn(HW2TaylorZacharyGame.java:52)at at   HW2TaylorZachary.main(HW2TaylorZachary.java:15)

3 个答案:

答案 0 :(得分:0)

您需要使用int中的非else。像,

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

}

答案 1 :(得分:0)

尝试in.next()而不是in.nextLine();并把它放在你的其他声明中

答案 2 :(得分:0)

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.