0
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)

Mozahler
  • 3,643
  • 6
  • 24
  • 42
  • 2
    please, provide the minimal, complete and verifiable example. [link](https://stackoverflow.com/help/mcve) – Praveenkumar Beedanal Nov 17 '18 at 09:30
  • Welcome to Stack Overflow! It looks like you may need to learn to use a debugger. Please help yourself to some [complementary debugging techniques](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/). If you still have issues afterwards, please [edit] your question to be more specific with what help you need. – Joe C Nov 17 '18 at 09:36
  • You can minimize the problem e.g. by only showing the square and exit option and removing all other functions. The question could be rephrased to: Why `Scanner.nextLine()` returns an empty extra line after `scanner.nextDouble()`? – michaeak Nov 17 '18 at 10:00
  • I suggest you step through the code in your debugger to see where it doesn't do what you expect. – Peter Lawrey Nov 17 '18 at 10:09
  • That's a lot of code. Maybe I'll try it out later, but IIRC, the `Scanner` sometimes returns the "line feed" as a separate token, so that when you enter `123.456[RETURN]`, it parses the `123.456` as a double and returns the `[RETURN]` as the next token (but I might be mistaken here, just a vague memory) – Marco13 Nov 18 '18 at 00:28
  • That actually sounds like the right thing to do. Empty strings are strings too, don't discriminate. Joking aside, you would expect the "cursor" to advance to the next character after the double, leaving the line ending characters. – Maarten Bodewes Nov 18 '18 at 00:36

0 Answers0