繁体   English   中英

什么是java.util.NoSuchElementException?如何解决?

[英]What is java.util.NoSuchElementException and how do i fix it?

我试图编写一个战舰游戏,但在提示用户输入他们的猜测坐标时遇到了这个错误:

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 game.User.takeTurn(User.java:121)
        at game.Game.main(Game.java:61)

这是发生错误的方法User.takeTurn():

    static void takeTurn() {
           System.out.println("Yout turn!");
           System.out.println("Enter your x guess:");
           Scanner scanInput = new Scanner(System.in);
           int x;
           x = scanInput.nextInt();
           System.out.println("You entered "+ x + ", Now enter your y     coord:");
           int y;
           y = scanInput.nextInt();
           System.out.println("You entered + " + y);
           if(Board.aiBoard[x][y] == 2) {
               System.out.println("Hit! You got a hit at: " + x + "," + y);
               Board.enemyBoardDisplay[x][y] = 'h';
               Board.aiBoard[x][y] = 3;
           }
           else {
            System.out.println("Miss!");
            Board.enemyBoardDisplay[x][y] = 'x';
            Board.aiBoard[x][y] = 1;
          }

           scanInput.close();
      }

我不太确定这是否重要,但我在课堂上也使用了另一台扫描仪。 如果您需要更多代码来帮助,请告诉我。 谢谢!

编辑1:好的,即使我做了scanInput.hasNextInt()之后,它也不起作用。 我还放入了else语句,该语句给出了xa值,但是现在x始终具有else语句给出的值。 它甚至要求输入仅默认为该值的输入。 但我不希望它成为默认值,我希望用户选择。

编辑2:这是我在主存根中调用代码的地方

while (gameOn == true) {
            if (turn == 0) {
                User.takeTurn();
                Board.sunkCheck();
                if (gameOn == false)
                    break;
            }

            else if (turn == 1) {
                Computer.takeTurn();
                Board.sunkCheck();
                if (gameOn == false)
                    break;
            }

这是我在较早之前尝试使用扫描仪的同一班级中使用扫描仪的地方:

System.out
                .println("\n Please enter the first x value for your ship  (Note, this will be the first point, from this point the \n"
                        + " ship can either go vertically downward or horizontally to the right so please choose the topmost point or leftmost \n"
                        + "point to do this. Also, the board is 10x10 but due to java array indexing the coordinates go x 0-9 y 0-9: ");
        try {
        Scanner coord = new Scanner(System.in);
        userX = coord.nextInt();
        System.out.println("You entered: " + userX);
        System.out.println("Now enter the y coordinate for this point: ");
        userY = coord.nextInt();
        System.out.println("You entered: " + userY);
        System.out
                .println("Please choose the direction of your ship. Enter v for verticle or h for horizontal(case sensitive): ");
        dir = (char) System.in.read();
        coord.close();
        }
        catch(InputMismatchException e) {
            System.out.println("Good job doof, since you tried to troll we did all the values for you!");
            userX = 3;
            userY = 3;
            dir = 'v';
        }

您可能需要检查next元素,然后再使用scanInput.hasNextInt()将其放入Scanner


更新:

如果使用上面的Scanner coord = new Scanner(System.in); 然后使用Scanner scanInput = new Scanner(System.in);调用该函数Scanner scanInput = new Scanner(System.in); 而不关闭coord Scanner ,那么您将遇到问题。

扫描仪将继续消耗数据流-这可能(将)导致意外的副作用。

您可能需要先关闭第一个,然后再使用另一个( 有关更多参考资料,请参见此处 )。


更新:

当您关闭coord.close()您正在关闭System.in并且当您尝试从中重新读取时,它将引发异常。

无需在每次需要输入时重新打开流,而是只需创建一次Scanner即可重复使用它。

当您尝试从扫描仪读取输入并且输入不存在时,会发生NoSuchElementException

因此,在接受输入之前,您应该检查扫描仪是否有一些输入供您使用,例如:

if (scanInput.hasNextInt()) {
    x = scanInput.nextInt();
}

暂无
暂无

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

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