我从System.in的输入中得到一个java.util.NoSuchElementException
我正在为一个程序编写代码,该程序显示一个基于x&y坐标的文件以及一个类型值的地图 我的代码正确显示ASCII映射,并允许我在映射上执行函数,但当代码最终返回到switch语句,在该语句中选择下一个选项时,它将跳过“option=input.nextLine();”行,并继续执行“input.nextLine();”的finally块给我一个Java.util.NoSuchElementException 有人知道这是为什么吗?我知道当枚举中没有这样的元素时,通常会发生这种异常,但我不知道为什么我的代码会跳过input.nextLine()行,然后在input.nextLine()上失败 只有在末尾的viewMap开关案例中激活Gorgon案例时,才会发生此错误。我尝试在“input.nextLine()”调用之前添加“input.nextLine()”调用,但没有任何区别 其中包括MapViewer程序中Switch语句的代码,以及用于构建和显示地图的代码。最后我还添加了Gorgon类我从System.in的输入中得到一个java.util.NoSuchElementException,java,oop,input,runtime-error,java.util.scanner,Java,Oop,Input,Runtime Error,Java.util.scanner,我正在为一个程序编写代码,该程序显示一个基于x&y坐标的文件以及一个类型值的地图 我的代码正确显示ASCII映射,并允许我在映射上执行函数,但当代码最终返回到switch语句,在该语句中选择下一个选项时,它将跳过“option=input.nextLine();”行,并继续执行“input.nextLine();”的finally块给我一个Java.util.NoSuchElementException 有人知道这是为什么吗?我知道当枚举中没有这样的元素时,通常会发生这种异常,但我不知道为什么我
public class MapViewerMenu {
public int option = 0;
public boolean complete = false;
Scanner input = new Scanner(System.in);
char [][] mapper = null;
Map currentMap = null;
User currentUser = null;
Vector <Grue> currentGrues = null;
public void run(){
while(!complete){
System.out.println("*******************");
System.out.println("* Map Viewer Menu *");
System.out.println("*******************");
System.out.println("1. Load Files");
System.out.println("2. Set Symbols");
System.out.println("3. View Map");
System.out.println("4. Scramble Map");
System.out.println("5. Reset Map");
System.out.println("6. Exit");
System.out.println("");
try{
option = input.nextInt();
}
catch(Exception e){
System.out.println("Error in input.");
System.out.println("Try again.");
System.out.println("");
}
finally{
// The Error occurs once the program exits viewMap and returns here.
//It skips the above "option = input.nextInt();" and comes down here and fails.
input.nextLine();
}
switch(option){
//removed irrelevant cases
case 3:
viewMap();
break;
case 6:
complete = true;
input.close();
break;
}
}
}
一旦它返回到类的原始switch语句,它就会失败,但它应该返回到开头并再次请求输入开关。通过进入Gorgon的boulder函数,而不是在系统上打开新的扫描仪,修复了错误。在中,我只是在参数中传递了现有的扫描仪 我在system.in上创建了另一个扫描仪并关闭了它,这可能与我的另一个扫描仪的故障有关
感谢所有人的帮助和时间。请创建一个显示您的问题的示例,这是太多的代码。添加了程序运行的概述,我不太熟悉创建SSCCE,因此我希望这对您有所帮助@Keppilt这是我在前面的评论中链接到的文档中的一个分步指南。哪些代码行是
(MapViewer.java:40)
和(MapViewerMenu.java:92)
指向的。请在代码中添加注释。@peeskillet在错误语句末尾添加了注释。
public void viewMap(){
//removed earlier code that built part of the map for readability
number = currentGrues.elements(); //this is an enumeration
while(number.hasMoreElements()){
temp3 = number.nextElement();
int sure =2;
switch(temp3.getName()){
case "Gorgon":
while(sure != 0){
System.out.printf("Would you like the Gorgon to change a square type?\n");
System.out.println("(0 for yes, 1 for no.)");
try{
sure = input.nextInt();
}
catch(Exception e){
System.out.println("Error in input.");
System.out.println("Try again.");
System.out.println("");
sure = 2;
}
finally{
input.nextLine();
}
if(sure == 0){
Gorgon gor = (Gorgon) temp3;
gor.boulder(currentMap, mapper);
after = true;
break;
}
else{
if(sure == 1){
break;
}
}
}
break;
// A boolean value after tells the program to reprint the map.
if(after){
System.out.println("Since some squares were changed, the updated map is printed.");
System.out.println("");
System.out.printf("Map Name: %s", currentMap.getName());
System.out.println("");
for(xpos = 0, ypos = 0; xpos < 16 && ypos < 16;){
System.out.print(mapper[xpos][ypos]);
xpos++;
if(xpos < 16){
continue;
}
else{
System.out.println("");
ypos++;
if(ypos < 16){
xpos = 0;
continue;
}
}
}
System.out.println("");
}
}
//This should then go back to the "option = input.nextInt" line, where it should ask the user for input, but doesnt.
import java.util.Scanner;
public class Gorgon extends Giant {
//Included is the function called by the viewMap switch statement.
//I added this because the error might exist here.
//other classes have similar functions, but perform just fine.
public void boulder(Map map, char[][] mapper){
int x = 0;
int y = 0;
Scanner input = new Scanner(System.in);
System.out.println("The Gorgon wants to turn an adjacent Square to stone!");
System.out.printf("Its position is (%d,%d).\n", this.currPos.col, this.currPos.row);
do{
do{ // Get an adjacent x coordinate
try{
System.out.println("Enter an x coordinate:");
x = input.nextInt();
}
catch(Exception e){
System.out.println("Error in input.");
System.out.println("Try again:");
System.out.println("");
}
finally{
input.nextLine();
}
}while(!(x >= currPos.col-1 && x <= currPos.col+1));
do{ // get an adjacent y coordinate
try{
System.out.println("Enter a y coordinate:");
y = input.nextInt();
}
catch(Exception e){
System.out.println("Error in input.");
System.out.println("Try again:");
System.out.println("");
}
finally{
input.nextLine();
}
}while(!(y >= currPos.row-1 && y <= currPos.row+1)); // keep asking for input while the input isn't within range.
}while(!((x >= currPos.col-1 && x <= currPos.col+1) && (y >= currPos.row-1 && y <= currPos.row+1)));
mapper[x][y] = 'B'; //Change the value in map data.
input.close(); //Close local input reader.
Exception in thread "main" java.util.NoSuchElementException: No line found
at java.util.Scanner.nextLine(Unknown Source)
at viewer.MapViewerMenu.run(MapViewerMenu.java:92) //points to the finally block at the beginning, where "input.nextLine()" is written.
at viewer.MapViewer.main(MapViewer.java:40) //This is just the original main that runs the program.