如何修复 "NoSuchElementException" 被抛出
How to fix "NoSuchElementException" being thrown
我创建了一个交互式 class 来解决运动方程问题(在用户输入循环中的另一个 class 中访问:mathiverse)效果很好,但在给出答案后抛出 NoSuchElementException
。
我试过移动我关闭扫描仪的位置,但没有用,我很确定问题出在我的输入循环中的 Kinematic class 功能上。
我的用户输入循环和运动学构造函数(不在同一个 class 内):
while(!(input.equals("exit")))
{
if(input.equals("help"))
{
System.out.println("~ Commands ~");
System.out.println("help - brings up a list of
commands(what you're reading)");
System.out.println("kinematic - solves a kinematic
equations problem; requires input of known and unknown
variables");
System.out.println("exit - closes the program");
System.out.println("~~~~~~~~~~~~");
}
//user decides to explore kinematic options
if(input.equals("kinematic"))
{
Kinematic calc = new Kinematic();
System.out.println(calc.answer());
}
input = scan.nextLine();
}
public Kinematic()
{
Scanner scanMath = new Scanner(System.in);
System.out.println("If you are solving for the variable, enter \"?\",
if the variable is not given, enter a space.");
System.out.println("Enter the acceleration: ");
acc = scanMath.nextLine();
System.out.println("Enter the displacement: ");
disp = scanMath.nextLine();
System.out.println("Enter the initial velocity: ");
init = scanMath.nextLine();
System.out.println("Enter the final velocity: ");
fin = scanMath.nextLine();
System.out.println("Enter the time: ");
time = scanMath.nextLine();
scanMath.close();
}
给出答案后,我希望我的代码继续搜索输入,但它抛出此消息:
Exception in thread "main" java.util.NoSuchElementException: No line
found at java.base/java.util.Scanner.nextLine(Scanner.java:1651) at
Mathiverse.main(Mathiverse.java:53)
从 Kinematic 关闭扫描仪时,您也会关闭 System.in 流。在 main 方法和 Kinematic 中使用相同的扫描仪。
看看:
java.util.NoSuchElementException - Scanner reading user input
我创建了一个交互式 class 来解决运动方程问题(在用户输入循环中的另一个 class 中访问:mathiverse)效果很好,但在给出答案后抛出 NoSuchElementException
。
我试过移动我关闭扫描仪的位置,但没有用,我很确定问题出在我的输入循环中的 Kinematic class 功能上。
我的用户输入循环和运动学构造函数(不在同一个 class 内):
while(!(input.equals("exit")))
{
if(input.equals("help"))
{
System.out.println("~ Commands ~");
System.out.println("help - brings up a list of
commands(what you're reading)");
System.out.println("kinematic - solves a kinematic
equations problem; requires input of known and unknown
variables");
System.out.println("exit - closes the program");
System.out.println("~~~~~~~~~~~~");
}
//user decides to explore kinematic options
if(input.equals("kinematic"))
{
Kinematic calc = new Kinematic();
System.out.println(calc.answer());
}
input = scan.nextLine();
}
public Kinematic()
{
Scanner scanMath = new Scanner(System.in);
System.out.println("If you are solving for the variable, enter \"?\",
if the variable is not given, enter a space.");
System.out.println("Enter the acceleration: ");
acc = scanMath.nextLine();
System.out.println("Enter the displacement: ");
disp = scanMath.nextLine();
System.out.println("Enter the initial velocity: ");
init = scanMath.nextLine();
System.out.println("Enter the final velocity: ");
fin = scanMath.nextLine();
System.out.println("Enter the time: ");
time = scanMath.nextLine();
scanMath.close();
}
给出答案后,我希望我的代码继续搜索输入,但它抛出此消息:
Exception in thread "main" java.util.NoSuchElementException: No line found at java.base/java.util.Scanner.nextLine(Scanner.java:1651) at Mathiverse.main(Mathiverse.java:53)
从 Kinematic 关闭扫描仪时,您也会关闭 System.in 流。在 main 方法和 Kinematic 中使用相同的扫描仪。
看看: java.util.NoSuchElementException - Scanner reading user input