0

I have a program that will calculate the area of a shape the user picks. I offer 4 shapes and after they go, I want to prompt them to either continue or quit. But, after the program asks for the shape and all, it stops and does not go onto any code after the first switch statement.

I tried making the code a part of the switch statement but then it won't run because of errors. I also tried to do another switch as you can see but nothing.

import java.util.Scanner;
import java.lang.Math;
public class area
{
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
String square;
String circle;
String rectangle;
String triangle;

System.out.println("Please choose one of the shapes you want the area of.");
System.out.println("Square, Circle, Rectangle, Triangle.");
String shape = scan.nextLine();
    switch (shape)
    {
        case "Square":
        System.out.println("Please enter the length of one side of the square.");
        double squareLength = scan.nextDouble();
        double squareArea = (squareLength * squareLength);
        System.out.println("The area of your square is "+ squareArea + ".");
        break;
        case "Circle":
        System.out.println("Please enter the radius of the circle.");
        double circleRadius = scan.nextDouble();
        double circleArea = (circleRadius * circleRadius)*Math.PI;
        System.out.println("The area of your circle is " + circleArea + ".");
        break;
        case "Rectangle":
        System.out.println("Please enter the width of the rectangle.");
        double rectangleWidth = scan.nextDouble();
        System.out.println("Please enter the length of the rectangle.");
        double rectangleLength = scan.nextDouble();
        double rectangleArea = (rectangleWidth * rectangleLength);
        System.out.println("The area of your rectangle is " + rectangleArea + ".");
        break;
        case "Triangle":
        System.out.println("Please enter the height of the trangle.");
        double triangleHeight = scan.nextDouble();
        System.out.println("Please enter the base of the trangle.");
        double triangleBase = scan.nextDouble();
        double triangleArea = (triangleHeight * triangleBase) / 2;
        System.out.println("The area of your triangle is " + triangleArea + ".");
        break;
        default:
        System.out.println("Please choose an option.");
    }
    System.out.println("Press c to continue and or q to quit.");
    String option = scan.nextLine();
    switch (option)
    {
        case "c":
        break;
        case "q":
        System.out.println("Have a good day!");
        break;
    }

    }
    }

Program runs only until the first switch statement, everything after isn't happening.

MC Emperor
  • 17,266
  • 13
  • 70
  • 106
Skorze
  • 9
  • 3

1 Answers1

-1

First of all, you need to put switch case into a while loop, in this way, while option is equals to "c", the program will execute. Second, the guy above put a reference Possible duplicate of Scanner is skipping nextLine() after using next() or nextFoo()?. So, when you read a value with nextDouble and then you try to read with nextLine, scan will skip the next nextLine. After this, you need to place another nextLine for the program to identify it.

public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        String option = "c";

        while(option.equals("c")){    
            System.out.println("Please choose one of the shapes you want the area of.");
            System.out.println("Square, Circle, Rectangle, Triangle.");
            String shape = scan.nextLine();
            switch (shape) {
                case "Square":
                    System.out.println("Please enter the length of one side of the square.");
                    double squareLength = scan.nextDouble();
                    double squareArea = (squareLength * squareLength);
                    System.out.println("The area of your square is " + squareArea
                            + ".");
                    break;
                case "Circle":
                    System.out.println("Please enter the radius of the circle.");
                    double circleRadius = scan.nextDouble();
                    double circleArea = (circleRadius * circleRadius) * Math.PI;
                    System.out.println("The area of your circle is " + circleArea
                            + ".");
                    break;
                case "Rectangle":
                    System.out.println("Please enter the width of the rectangle.");
                    double rectangleWidth = scan.nextDouble();
                    System.out.println("Please enter the length of the rectangle.");
                    double rectangleLength = scan.nextDouble();
                    double rectangleArea = (rectangleWidth * rectangleLength);
                    System.out.println("The area of your rectangle is "
                            + rectangleArea + ".");
                    break;
                case "Triangle":
                    System.out.println("Please enter the height of the trangle.");
                    double triangleHeight = scan.nextDouble();
                    System.out.println("Please enter the base of the trangle.");
                    double triangleBase = scan.nextDouble();
                    double triangleArea = (triangleHeight * triangleBase) / 2;
                    System.out.println("The area of your triangle is " + triangleArea
                            + ".");
                    break;
                default:
                    System.out.println("Please choose an option.");
                    break;

            }
            scan.nextLine();
            System.out.println("Press c to continue and or q to quit.");
            option = scan.nextLine();
        }
}
Hasho
  • 79
  • 5