什么是java.util.NoSuchElementException?如何解决? [英] What is java.util.NoSuchElementException and how do i fix it?
问题描述
我正在尝试对战舰游戏进行编程,并在提示用户输入其猜测坐标的时候遇到了此错误:
I was trying to program a game of battleship and i ran into this error at the point where the user was prompted to enter their guess coordinates:
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()方法:
Here is the method User.takeTurn() where the error occured:
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();
}
我不确定是否很重要,但我也早先使用了另一台扫描仪在课堂里。如果您需要更多代码来帮助,请告诉我。谢谢!
Im not really sure if it matters or not but i also use another scanner earlier in the class. If you need more code to help just let me know. Thanks!
编辑1:
好吧,即使在我执行scanInput.hasNextInt()之后,它也不起作用。我还放入了else语句,该语句为x提供了一个值,但现在x始终具有else语句提供的值。它甚至要求输入仅默认为该值的输入。但是我不想让它成为默认值,我希望用户选择。
edit 1: Ok, even after I do scanInput.hasNextInt() its not working. I also put in an else statement that gave x a value but now x always has the value that the else statement gives. It dosent even ask for an input it just defaults to that value. But i dont want it to go to a default i want the user to pick.
编辑2:这是我在主存根中调用代码的地方
edit 2: This is where i call the code in my main stub
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;
}
这是我在与IM尝试使用的同一班级中较早使用扫描仪的地方立即使用扫描仪:
This is where i use a scanner earlier in the same class as im trying to use the scanner now:
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
元素,然后再将 Scanner
放入 Scanner
中。 code> scanInput.hasNextInt()。
You might want to check for next
element before getting it in Scanner
with scanInput.hasNextInt()
.
已更新:
如果您使用上述 Scanner coord = new Scanner(System.in);
,然后调用 Scanner scanInput = new Scanner(System.in);
而不关闭 coord
扫描器
,那么您将遇到问题。
If you use the above Scanner coord = new Scanner(System.in);
and then call the function with Scanner scanInput = new Scanner(System.in);
without closing the coord
Scanner
, then you will be having issues.
扫描器将继续t o消耗数据流-可能(将)导致
意外的副作用。
Scanners will continue to consume the stream - this may (will) lead to unexpected side-effects.
您可能想关闭使用第一个之前(此处提供更多参考 )。
You may want to close the first one before using another (here for more ref).
更新:
当您关闭 coord.close()
时,您正在关闭 System.in
,并且当您尝试重新阅读时从它,它将抛出异常。
When you close coord.close()
you are closing System.in
and when you try to re-read from it, it will throw the exception.
您不必每次都需要重新打开流,而只需创建一次 Scanner
,然后重新使用它。
Instead of re-opening the stream every time you need input you could create your Scanner
once and just re-using it.
这篇关于什么是java.util.NoSuchElementException?如何解决?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!