0

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?
solorzke
  • 43
  • 3
  • It is becuause of this `in.nextInt();` this contains a string character `\n` So it will skip next `in.nextLine()` You want to try putting `in.nextLine()` after the `in.nextInt()` – Smit Mar 02 '17 at 04:51
  • There is a missing `break` statement at the end of `case "yes"`. – user207421 Mar 02 '17 at 05:13
  • @Smit You have that completely back to front. `nextInt()` does *not* contain a newline, and will *not* skip the next `nextLine()`, so the requirement is to consume the newline by adding a `nextLine()` call. Your explanation makes no sense. – user207421 Mar 02 '17 at 05:14
  • @EJP Thanks for correcting me. Appreciate your feedback. Will take note from the next time. – Smit Mar 02 '17 at 05:17

1 Answers1

0

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.

badjr
  • 1,913
  • 2
  • 20
  • 28