为什么这个错误 Exception in thread "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((,但没有下一行,因此"未找到行";在错误消息中。试着在输入.nextLine((周围使用if (input.hasNextLine())来检查它是否首先存在。

以下是修复代码:您没有正确使用scanner。

import javassist.bytecode.stackmap.BasicBlock;
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(){
int choice = -1;
do{
Scanner input = new Scanner(System.in);
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(" ");
Scanner scanner = new Scanner(System.in);
try  {
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);
}catch (Exception e){
scanner.close();
}
}
private void areaofRectangle(){
System.out.println("+----------------------------+");
System.out.println("|  **Area of the Rectangle** |");
System.out.println("+----------------------------+");
System.out.println(" ");
Scanner scanner = new Scanner(System.in);
try {
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);
}catch (Exception e){
scanner.close();
}
}
private void areaofTriangle(){
System.out.println("+----------------------------+");
System.out.println("|  **Area of the Triangle**  |");
System.out.println("+----------------------------+");
System.out.println(" ");
Scanner scanner = new Scanner(System.in);
try {
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);
} catch (Exception e){
scanner.close();
}
}
private void areaofCircle(){
System.out.println("+----------------------------+");
System.out.println("|   **Area of the Circle**   |");
System.out.println("+----------------------------+");
System.out.println(" ");
Scanner scanner = new Scanner(System.in);
try {
System.out.println("Enter the radius:");
double r= scanner.nextDouble();
double  area=(22*r*r)/7 ;
System.out.println("Area of Circle is: " + area);
}catch (Exception e){
scanner.close();
}
}
}

相关内容

最新更新