I've been stuck with this error anybody can figure this out? I keep getting an error right away. I'm thinking it is a simple fix, but I can't figure it out. I've been fixing this a while and I don't know where. Thank you for you help.
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);
}
}
}
I've gone through research the error reports and the Java-docs and finding reports with similar problems. Please help.
I get this error
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.