Search code examples
javawhile-loopswitch-statementcasedefault

While loop skips switch cases and executes default


The program should run calculations for different shapes as different cases nested inside a while loop. Heres the code:

package Lab_7;
import java.util.*;

public class compar {
    public static void main(String [] args){
        Scanner d = new Scanner(System.in);
        boolean start = true;


    while(start){
        System.out.print("Would you like to start the program?: ");
        String answer1 = d.nextLine();

        switch (answer1){
            case "yes":
                System.out.println("Which shape would you like to use to compute area/perimeter?: ");
                String answer2 = d.nextLine();  

                if(answer2.equals("circle")){           
                    try{
                        System.out.print("Enter radius: ");
                        int answer3 = d.nextInt();
                        Circle c = new Circle(answer3);
                        double area = c.computeArea();
                        double perimeter = c.computePerimeter();
                        System.out.println("Area = " + area + " & perimter = " + perimeter );
                        break;                          
                    }
                    catch(Exception e){
                        System.out.println("Error!");
                        break;
                    }
                }

            case "no":
                System.out.println("Program Terminating...");
                start = false;
                break;

            default:
                System.out.println("bug");
                continue;
            }
    }
    d.close();
}

}

However, after running the first successful run, the program should loop back to the beginning (asking the user to start the program?) but instead this happens:

Would you like to start the program?: yes
Which shape would you like to use to compute area/perimeter?: 
circle

Enter radius: 10

Area = 314.16 & perimter = 62.832

Would you like to start the program?: bug

Would you like to start the program?: 

I could use a bunch of if-statements but I really need to know why after the first successful run, my program:

  1. Skips all cases and executes the default statement, then loops back to the first print statement and finally waits for input?

Solution

  • When you enter the radius, d.nextInt() consumes the next int, but not the new line.

    After the area is computed, the break statement terminates the switch statement.

    Then the line String answer1 = d.nextLine() consumes the new line that d.nextInt() didn't consume, which causes it to execute the default case because answer1 was neither "yes" or "no".

    The continue causes the execution to go back to the start of the while loop, where it then waits for input again.

    To fix it, add d.nextLine() after getting input for the radius:

    int answer3 = d.nextInt();
    d.nextLine(); //consumes the \n character
    

    Also, you have to add a break to the end of the yes case. Otherwise, the user could enter "yes" and then something other than "circle", and the program execution would fall through to the no case and terminate.