Java 使用扫描仪时出现NoTouchElement异常
我试图在程序中使用Java 使用扫描仪时出现NoTouchElement异常,java,java.util.scanner,nosuchelementexception,system.in,Java,Java.util.scanner,Nosuchelementexception,System.in,我试图在程序中使用Scanners第二次从控制台获取输入,但在另一种方法中调用第二次Scanner时,它出现了NoTouchElement异常 public class Garden { public static final Garden GARDEN = new Garden(); //variable declartaions removed public static void main(String[] args) { if (null
Scanner
s第二次从控制台获取输入,但在另一种方法中调用第二次Scanner
时,它出现了NoTouchElement异常
public class Garden {
public static final Garden GARDEN = new Garden();
//variable declartaions removed
public static void main(String[] args) {
if (null != args && 0 < args.length) {
GARDEN.fileName = args[0].trim();
}
if (GARDEN.fileName != null) {
GARDEN.fileReader(GARDEN.fileName);
} else {
GARDEN.fileReader();
}
GARDEN.startMenu();
int mainI = 0;
while (mainI != 1000000) {
try {
Thread.sleep(0);
} catch (InterruptedException e) {
}
GARDEN.daysWeather();
GARDEN.anotherDay();
mainI++;
}
}
protected void fileReader() { // asks for file name for config file
System.out.println("Enter File Name Please");
Scanner cmdReader = null;
String cmdInput = null;
try {
cmdReader = new Scanner(System.in);
cmdInput = cmdReader.nextLine();
} catch (NoSuchElementException noSuchElement) {
noSuchElement.printStackTrace();
fileReader(); //throwing error here
}
//code removed
}
protected void startMenu() {// modified code from ATM lab (week2)
int selected = 0;
//code removed
Scanner climateScanner = new Scanner(System.in);
System.out.println("Select a number 1-4");
selected = climateScanner.nextInt();
switch (selected) {
case 1: // semiarid
weatherType = 10; //10% chance to rain
climateScanner.close();
break;
case 2: // arid
weatherType = 20; //5% chance to rain
climateScanner.close();
break;
case 3:
weatherType = 50; //2% chance to rain
tropicalWeather = true;
climateScanner.close();
break;
case 4:
weatherType = 20;//5% chance to rain
storming = true;
climateScanner.close();
break;
default:
System.out.println("Invalid Input try again");
startMenu(); //using Recursion to ask for input again
break;
}
//code removed
}
}
如果我从运行fileReader()
时删除startMenu()
,它会工作,但是由于某种原因,在运行后会抛出异常
public class Garden {
public static final Garden GARDEN = new Garden();
//variable declartaions removed
public static void main(String[] args) {
if (null != args && 0 < args.length) {
GARDEN.fileName = args[0].trim();
}
if (GARDEN.fileName != null) {
GARDEN.fileReader(GARDEN.fileName);
} else {
GARDEN.fileReader();
}
GARDEN.startMenu();
int mainI = 0;
while (mainI != 1000000) {
try {
Thread.sleep(0);
} catch (InterruptedException e) {
}
GARDEN.daysWeather();
GARDEN.anotherDay();
mainI++;
}
}
protected void fileReader() { // asks for file name for config file
System.out.println("Enter File Name Please");
Scanner cmdReader = null;
String cmdInput = null;
try {
cmdReader = new Scanner(System.in);
cmdInput = cmdReader.nextLine();
} catch (NoSuchElementException noSuchElement) {
noSuchElement.printStackTrace();
fileReader(); //throwing error here
}
//code removed
}
protected void startMenu() {// modified code from ATM lab (week2)
int selected = 0;
//code removed
Scanner climateScanner = new Scanner(System.in);
System.out.println("Select a number 1-4");
selected = climateScanner.nextInt();
switch (selected) {
case 1: // semiarid
weatherType = 10; //10% chance to rain
climateScanner.close();
break;
case 2: // arid
weatherType = 20; //5% chance to rain
climateScanner.close();
break;
case 3:
weatherType = 50; //2% chance to rain
tropicalWeather = true;
climateScanner.close();
break;
case 4:
weatherType = 20;//5% chance to rain
storming = true;
climateScanner.close();
break;
default:
System.out.println("Invalid Input try again");
startMenu(); //using Recursion to ask for input again
break;
}
//code removed
}
}
公共类花园{
公共静态最终花园=新花园();
//取消可变申报单
公共静态void main(字符串[]args){
if(null!=args&&0
你不能用这种方式访问它。您必须初始化该类或使您的方法成为静态的。还有什么是花园
好的,现在您已经编辑了代码
再次
问题是两个不同的扫描器(一个在startMenu()中,一个在readFile()中)更改了它,所以类变量中有一个Scanner Scanner=new Scanner(System.in),然后从方法中调用Scanner.nextLine(),或者使
startMenu
静态。fileReader(GARDEN.fileName)只是fileReader()的另一个实例在控制台中启动程序时,java Garden testA.txt将运行fileReader(文件名)java Garden将运行fileReader()两个fileReader()之间的差异方法是,获取参数的方法从参数获取文件名,而不带参数的方法通过扫描仪从控制台输入获取文件名(不确定这是否是您的意思,我是否有另一个同名方法)@user1642671我建议您使用调试程序
。然后,您将很容易意识到我尝试过的问题:它获取cmdReader=new Scanner(System.in);然后抛出异常是否每次使用后都与System.in有关?GARDEN是创建的单例吗?我不认为这是问题所在,因为它在未调用GARDEN.startMenu()时与GARDEN.fileReader()一起工作
GARDEN.fileReader(GARDEN.fileName); // you are parsing input argument
// But method in your class is no argument method