如何解决java.util.NoSuchElementException
错误?我调用了扫描程序对象3次,程序将接受前两次输入。但是在第三次尝试时,我立即收到java.util.NoSuchElementException
错误。扫描程序对象的名称为stdInput
。
我曾尝试为引发该错误的实例创建一个新的扫描仪对象,但是我仍然从代码的另一行中得到相同的错误。
/**
* reddit url: https://www.reddit.com/r/dailyprogrammer/comments/pjbj8/easy_challenge_2/
*/
import java.util.Scanner;
public class challenge {
public static void main(String[] args) {
int choiceNum = 0;
boolean continueRunningProgram = true;
String repeatProgram = "";
Scanner stdInput = new Scanner (System.in);
do {
System.out.println("Are you solving for force (1), mass (2), or acceleration (3)?");
choiceNum = stdInput.nextInt();
if(isValidChoiceNum(choiceNum) == false) {
do {
System.out.println("The number " + choiceNum + " is an invalid choice. Please choose again.");
choiceNum = stdInput.nextInt();
} while(isValidChoiceNum(choiceNum) == false);
}
switch(choiceNum) {
case 1:
System.out.println("The force is " + solvingForForce());
break;
case 2:
System.out.println("The mass is " + solvingForMass());
break;
case 3:
System.out.println("The acceleration is " + solvingForAcceleration());
break;
}
System.out.println("Would you like to solve another problem involving force, mass, and acceleration (Y/N)?");
repeatProgram = stdInput.next();
if(isValidChoiceChar(repeatProgram) == false) {
do {
System.out.println("The letter " + repeatProgram + " is an invalid choice. Please choose again.");
repeatProgram = stdInput.next();
} while(isValidChoiceChar(repeatProgram) == false);
}
if(repeatProgram.compareTo("Y") == 0) {
continueRunningProgram = true;
} else {
continueRunningProgram = false;
}
} while(continueRunningProgram == true);
stdInput.close();
} // end of main method
public static boolean isValidChoiceNum(int c) {
if(c < 1 || c > 3 ) {
return false;
} else {
return true;
}
}
public static boolean isValidChoiceChar(String c) {
if(c.compareTo("Y") == 0 || c.compareTo("N") == 0) {
return true;
} else {
return false;
}
}
public static double solvingForForce() {
Scanner stdInput2 = new Scanner (System.in);
System.out.println("Please enter a value for mass.");
double m = stdInput2.nextDouble();
System.out.println("Please enter a value for acceleration.");
double a = stdInput2.nextDouble();
stdInput2.close();
return m * a;
}
public static double solvingForMass() {
Scanner stdInput2 = new Scanner (System.in);
System.out.println("Please enter a value for force.");
double f = stdInput2.nextDouble();
System.out.println("Please enter a value for acceleration.");
double a = stdInput2.nextDouble();
stdInput2.close();
return f / a;
}
public static double solvingForAcceleration() {
Scanner stdInput2 = new Scanner (System.in);
System.out.println("Please enter a value for force.");
double f = stdInput2.nextDouble();
System.out.println("Please enter a value for mass.");
double m = stdInput2.nextDouble();
stdInput2.close();
return f * m;
}
} // end of class
答案 0 :(得分:1)
不要再疯狂关闭链接到Scanner
的{{1}}了!它将关闭基础流(System.in
),从而导致任何其他尝试从该System.in
读取的操作都引发异常。
是的,您会收到一条警告,但是可以忽略该警告,也可以添加
Stream
为避免这种情况。
请记住,如果您没有打开资源,则不应关闭它。让JVM在终止程序时将其关闭。
这还包括在带有资源块的try中创建@SuppressWarnings("resource")
Scanner in = new Scanner(System.in);
:
Scanner(System.in)
这将隐式关闭该块末尾的流。
try-with-resources语句可确保在语句末尾关闭每个资源。
此外,您还有几个try(Scanner in = new Scanner(System.in)){
//Code...
}
的对象从Scanner
中读取,这是一种不好的做法。我会将它们作为参数传递给您的方法:
System.in
还请注意,如果您要执行以下结构:
public static double solvingForMass(Scanner in) {
System.out.println("Please enter a value for force.");
double f = in.nextDouble();
System.out.println("Please enter a value for acceleration.");
double a = in.nextDouble();
return f / a;
}
可以简化为
if(someBool)
return true;
else
return false;
因此,您的return someBool
方法可以简化为:
isValidChoice()
尽管请注意,您可以使用public static boolean isValidChoiceChar(String c) {
return c.compareTo("Y") == 0 || c.compareTo("N") == 0;
}
和equalsIgnoreCase()equals()
String`s