gpt4 book ai didi

java - 修复 Scanner 的 java.util.NoSuchElementException

转载 作者:行者123 更新时间:2023-11-30 05:48:37 26 4
gpt4 key购买 nike

如何解决 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

最佳答案

停止关闭链接到 System.inScanner 的疯狂行为!它将关闭底层流 (System.in),导致任何其他从该 Stream 读取的尝试抛出异常。

是的,您会收到警告,但忽略该警告是安全的,或者您可以添加

@SuppressWarnings("resource")
Scanner in = new Scanner(System.in);

为了避免它。

请记住,如果您没有打开资源,则不应关闭它。让 JVM 在终止程序时关闭它。

<小时/>

这还包括在 try with resources block 内创建一个 Scanner(System.in):

try(Scanner in = new Scanner(System.in)){
//Code...
}

这将在 block 末尾隐式关闭流。

来自Java tutorials :

The try-with-resources statement ensures that each resource is closed at the end of the statement.

<小时/>

此外,您还有多个从 System.in 读取的 Scanner 对象,这是不好的做法。我会将它们作为参数传递给您的方法:

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;
}

尽管请注意,您可以使用 equals() 和 equalsIgnoreCase()方法来比较String`的

关于java - 修复 Scanner 的 java.util.NoSuchElementException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54393770/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com