import java.util.Scanner;
import java.lang.Math;
public class ClassNine {
public static Scanner INPUT = new Scanner(System.in);
public static void nine() {
boolean loop = true;
while(loop) {
loop = areaMenu();
}
Seperator.addLine();
}
public static boolean areaMenu() {
boolean loop2 = true;
boolean validChoice = false;
while(loop2) {
System.out.print("\n1 - Triangle\n"
+ "2 - Square\n"
+ "3 - Rectangle\n"
+ "4 - Parallelogram\n"
+ "5 - Trapezium\n"
+ "6 - Circle\n"
+ "7 - Ellipse\n"
+ "8 - Sector\n"
+ "\t>>enter 'exit' to leave.\n"
+ "\nChoice: ");
String choice = INPUT.nextLine();
if(choice.matches("[0-8]")) {
validChoice = true;
}else if(choice.matches("exit")){
return false;
}else{
System.out.println("Invalid Input. Exiting 3."); //TODO
validChoice = false;
}
if(validChoice) {
try {
switch(choice){
case "1": triangle();
break;
case "2": square();
break;
case "3": rectangle();
break;
case "4": parallelogram();
break;
case "5": trapezium();
break;
case "6": circle();
break;
case "7": ellipse();
break;
case "8": sector();
break;
case "exit": loop2 = false;
break;
}
}catch(Exception e) {
System.out.println("Invalid Input. Exiting.");
break;
}
}
}
return true;
}
public static void triangle() {
System.out.print("\nTriangle Base: ");
double base = INPUT.nextDouble();
System.out.print("Triangle Height: ");
double height = INPUT.nextDouble();
System.out.println("\n\nArea of Traingle: " + 0.5*base*height + "\n");
}
public static void square() {
System.out.print("\nSquare Side Lenght: ");
double side = INPUT.nextDouble();
System.out.println("\n\nArea of Square: " + Math.pow(side, 2) + "\n");
}
public static void rectangle() {
System.out.print("\nRectangle Lenght: ");
double lenght = INPUT.nextDouble();
System.out.print("Rectangle Height: ");
double height = INPUT.nextDouble();
System.out.println("\n\nArea of Rectangle: " + lenght*height + "\n");
}
public static void parallelogram() {
System.out.print("\nParallelogram Lenght: ");
double lenght = INPUT.nextDouble();
System.out.print("Parallelogram Height: ");
double height = INPUT.nextDouble();
System.out.println("\n\nArea of Parallelogram: " + lenght*height + "\n");
}
public static void trapezium() {
System.out.print("\nTrapezium Narrow Width: ");
double narrow = INPUT.nextDouble();
System.out.print("Trapezium Wide Width: ");
double wide = INPUT.nextDouble();
System.out.print("Trapezium Height: ");
double height = INPUT.nextDouble();
System.out.println("\n\nArea of Trapezium: " + 0.5*(narrow + wide)*height + "\n");
}
public static void circle() {
System.out.print("\nCircle Radius: ");
double radius = INPUT.nextDouble();
System.out.println("\n\nArea of Circle: " + radius*Math.pow(Math.PI, 2) + "\n");
}
public static void ellipse() {
System.out.print("\nEllipse Narrow Radius: ");
double narrow = INPUT.nextDouble();
System.out.print("Ellipse Wide Radius: ");
double wide = INPUT.nextDouble();
System.out.println("\n\nArea of Ellipse: " + Math.PI*narrow*wide + "\n");
}
public static void sector() {
System.out.print("\nSector Radius: ");
double radius = INPUT.nextDouble();
System.out.print("Value of angle subtending the sector (in radians): ");
double angle = INPUT.nextDouble();
System.out.println("\n\nArea of sector: " + 0.5*Math.pow(radius, 2)*angle + "\n");
}
}
After picking an option and going through the associated method the program loops and displays through the options menu again, as it should, but because without any further input the INPUT.nextLine()
returns an empty string and goes directly to the 'else' statement containing "Exiting 3" before looping one more time and actually waiting for the user prompt.
Why does Scanner.nextLine()
return an empty extra line after scanner.nextDouble()
?
Another semi-issue is that inputting "exit" does what it should and exits the program but looking at the code it shouldn't end the program but instead just loop over the menu again since it breaks the loop which returns true to start the loop again. Right? (rhetorical question, i'm obviously missing something)