什么是java.util.NoSuchElementException以及如何修复它?
我试图编写一个战舰游戏,在提示用户输入其猜测坐标时,我遇到了这个错误:什么是java.util.NoSuchElementException以及如何修复它?,java,eclipse,exception,nosuchelementexception,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)
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语句,它给了x一个值,但是现在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';
}
在尝试从扫描仪读取输入以及输入不存在时发生
因此,在获取输入之前,您应该检查扫描仪是否有一些输入供您使用,如:
if (scanInput.hasNextInt()) {
x = scanInput.nextInt();
}
在使用
scanInput.hasNextInt()
将元素放入Scanner
之前,您可能需要检查next
元素
更新: 如果使用上述
扫描仪坐标=新扫描仪(System.in)
然后调用函数,使用Scanner scanInput=new Scanner(System.in)如果不关闭坐标
扫描仪
,则会出现问题
扫描仪将继续使用该流-这可能(将)导致
意外的副作用
您可能需要先关闭第一个,然后再使用另一个()
更新:
当您关闭coord.close()
时,您正在关闭System.in
并且当您尝试从中重新读取时,它将抛出异常
您不必每次需要输入时都重新打开流,只需创建一次扫描仪,然后重新使用即可。您是否查看了?Quote:“通过各种访问器方法抛出,以表明所请求的元素不存在。”以及“抛出:NoSuchElementException-如果输入已耗尽”的文档,该文档应为scanInput
not scannerHmm,当我这样做时,ide使所有之后的代码都有一个错误,因为x可能没有值。我做了一个else语句,它给了x一个值,但是现在x总是有else语句给的值。是什么原因导致扫描仪没有可使用的输入?没错。因此,初始化x时,默认值为0,即intx=0代码>。您可以在去极化时将值设置为x,但这不是我想要做的。。。我希望用户选择x的输入,但由于某些原因,它不起作用。有人能告诉我为什么吗?啊,好主意,但我已经关闭了第一个,我做了coord.close();等等,很抱歉没有点击你的链接,我想我知道该怎么办了。我可以用以前在课堂上用过的扫描器吗?@AshwinGupta你可以把它传给function@AshwinGupta试着只传递一个扫描仪
,如果它解决了你的问题,试一下。好的,试过了,我现在遇到的问题是在我关闭类文件末尾的扫描器后,它说:令牌关闭预期标识符上的语法错误。