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

时间:2015-01-31 06:55:26

标签: java eclipse exception nosuchelementexception

我正在尝试编写战舰游戏,并且在提示用户输入猜测坐标的位置遇到此错误:

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()后它也无法正常工作。我还输入了一个给出x值的else语句,但现在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';
        }

2 个答案:

答案 0 :(得分:3)

您可能需要检查next元素,然后ScannerscanInput.hasNextInt()一起使用。


<强>更新

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

  

扫描仪将继续消耗流 - 这可能(将)导致   意想不到的副作用。

您可能想要在使用另一个(here for more ref)之前关闭第一个。


<强>更新

当您关闭coord.close()时,您正在关闭System.in,当您尝试重新读取它时,它将抛出异常。

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

答案 1 :(得分:1)

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

NoSuchElementException

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

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