简体   繁体   English

使用扫描仪时出现 java.util.NoSuchElementException

[英]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. (我在测试时总是输入 2)我尝试删除 do-while 循环,但这并没有解决问题。 Thank you谢谢

Better if can use one Scanner to read from System.in for the whole app如果可以使用一个扫描仪从 System.in 读取整个应用程序会更好

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关闭接收播放器的扫描器将关闭 InputStream,这将导致此 NoSuchElmentException.Better 如果您可以定义一个扫描器并在两种情况下使用它来获取输入

As the other guy said, I finally found the issue.正如另一个人所说,我终于找到了问题所在。 Closing the scanner once closed the InputStream and so it couldn't be used again.关闭扫描器一旦关闭 InputStream,就无法再次使用。 I had to make one scanner at the start of my main and pass it to all the functions that needed it.我必须在我的主程序开始时制作一个扫描仪,并将它传递给所有需要它的功能。 I removed all the closes from each function, and just closed at the end of main.我删除了每个函数的所有关闭,并在 main 结束时关闭。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM