java - 为什么线程“main”java.util.NoSuchElementException中出现此错误异常:
问题描述
我一直被这个错误困住,任何人都可以解决这个问题吗?我不断收到错误消息。我认为这是一个简单的修复,但我无法弄清楚。我已经修复了一段时间,我不知道在哪里。谢谢你的帮助。
import java.util.Scanner;
public class Menu {
boolean exit;
public static void main(String[] args){
Menu menu = new Menu();
menu.runMenu();
}
public void runMenu(){
printHeader();
while(!exit){
printMenu();
int choice = getInput();
performAction(choice);
}
}
private void printHeader(){
System.out.println("+----------------------------+");
System.out.println("| Area Plane of Shape |");
System.out.println("+----------------------------+");
}
private void printMenu(){
System.out.println(" ");
System.out.println("Please make selection: ");
System.out.println(" ");
System.out.println("1) Area of the Square");
System.out.println("2) Area of the Rectangle");
System.out.println("3) Area of the Triangle");
System.out.println("4) Area of the Circle");
System.out.println("0) Exit");
}
private int getInput(){
Scanner input = new Scanner(System.in);
int choice = -1;
do{
System.out.println(" ");
System.out.println("Enter your choice: ");
try {
choice = Integer.parseInt(input.nextLine());
} catch(NumberFormatException e){
System.out.println("Invalid Selection. Please try again.");
}
}while(choice < 0 || choice > 4);
return choice;
//return 1;
}
private void performAction(int choice){
switch(choice){
case 0:
exit = true;
System.out.println("Thank you for using my application.");
break;
case 1:
areaofSquare();
break;
case 2:
areaofRectangle();
break;
case 3:
areaofTriangle();
break;
case 4:
areaofCircle();
break;
default:
System.out.println("Invalid");
}
}
private void areaofSquare(){
System.out.println("+----------------------------+");
System.out.println("| **Area of the Square** |");
System.out.println("+----------------------------+");
System.out.println(" ");
try (Scanner scanner = new Scanner(System.in)) {
System.out.println("Enter Side of Square: ");
// Storing the captured value in a variable
double side = scanner.nextDouble();
// Area of Square = side*side
double area = side * side;
System.out.println("Area of Square is: " + area);
}
}
private void areaofRectangle(){
System.out.println("+----------------------------+");
System.out.println("| **Area of the Rectangle** |");
System.out.println("+----------------------------+");
System.out.println(" ");
try (Scanner scanner = new Scanner(System.in)) {
System.out.println("Enter the length of Rectangle:");
double length = scanner.nextDouble();
System.out.println("Enter the width of Rectangle:");
double width = scanner.nextDouble();
// Area = length*width;
double area = length * width;
System.out.println("Area of Rectangle is: " + area);
}
}
private void areaofTriangle(){
System.out.println("+----------------------------+");
System.out.println("| **Area of the Triangle** |");
System.out.println("+----------------------------+");
System.out.println(" ");
try (Scanner scanner = new Scanner(System.in)) {
System.out.println("Enter the width of the Triangle:");
double base = scanner.nextDouble();
System.out.println("Enter the height of the Triangle:");
double height = scanner.nextDouble();
// Area = (width*height)/2
double area = (base * height) / 2;
System.out.println("Area of Triangle is: " + area);
}
}
private void areaofCircle(){
System.out.println("+----------------------------+");
System.out.println("| **Area of the Circle** |");
System.out.println("+----------------------------+");
System.out.println(" ");
try(Scanner scanner = new Scanner(System.in)) {
System.out.println("Enter the radius:");
double r= scanner.nextDouble();
double area=(22*r*r)/7 ;
System.out.println("Area of Circle is: " + area);
}
}
}
我已经研究了错误报告和 Java 文档,并找到了具有类似问题的报告。请帮忙。
我收到这个错误
Exception in thread "main" java.util.NoSuchElementException: No line found
at java.base/java.util.Scanner.nextLine(Scanner.java:1651)
at Menu.getInput(Menu.java:50)
at Menu.runMenu(Menu.java:20)
at Menu.main(Menu.java:9)
Command execution failed.
解决方案
在您的 getInput 方法中,您尝试调用 input.nextLine() 并且没有下一行,因此错误消息中出现“找不到行”。尝试if (input.hasNextLine())
在 input.nextLine() 周围使用一个来检查它是否首先存在。
推荐阅读
- angular - 检测路由更改和 url 条目的差异
- javascript - 如何通过一次提交将两个表字段发送到数据库?
- android - 如何允许 new View.OnClickListener() 在 android studio 中运行?
- angular - 如何在 Angular 12 中导入默认导出的 graphql 查询?
- asp.net - 如何在“主布局”中使用“用户面板布局”?
- javascript - 反应加密密码
- ios - 为什么子类化主 ViewController 的视图会导致委托永远不会触发?
- android - 如何在相机应用程序上显示文本而不记录它#Android #Camera API
- meson-build - 介子:连接两个没有'/'的字符串
- docker - 带有 docker 的烧瓶 reqparser.parse_args 传递省略号对象 - 错误