twjohns29
twjohns29

Reputation: 125

Compile error in my do/while statements?

I created 3 classes Circle, Rectangle, and RightTriangle using my FigureGeometry interface. This program is supposed to take input run calculation using the different classes.

I'm getting a couple of errors with my do/while statement. The do statement says "Illegal start of type". My while statement say the same and "can't find variable option".

I've tried searching for other questions on here/google, but I can't find anything that has helped with my code. It is always something like an extra bracket but as far as I can tell there isn't anything extra here.

I've double checked that all my open/close brackets match up. I've tried having my option variable in the do statement (which I was certain wasn't correct, but I was just trying some different things). I'm just not sure what the issue is?

package figuregeometry;

import java.util.Scanner;
public class FigGeometryCalculator
{

    //========================MAIN==============================================
    public static void main(String[] args)
    {

        float height;
        float width;
        float length;
        float radius;
        float degrees;

        String menu = "Would you like to figure the geometry for a: " + "\n"
        + "1. Circle" + "\n" 
        + "2. Rectangle" + "\n"
        + "3. Right Triangle" + "\n"
        + "0. Exit";

        do
        {
            Scanner in = new Scanner(System.in);
            int option = in.nextInt();

            switch(option)
            {
                case 1: System.out.println("Enter radius: ");
                        radius = in.nextFloat();
                        System.out.println("Enter Degrees of sector: ");
                        degrees = in.nextFloat();
                        Circle newCircle = new Circle(radius, degrees);

                        System.out.println("Set Scale: ");
                        newCircle.setScale(in.nextInt());
                        newCircle.toString();
                        break;

                case 2: System.out.println("Enter length: ");
                        length = in.nextFloat();
                        System.out.println("Enter width: ");
                        width = in.nextFloat();
                        Rectangle newRectangle = new Rectangle(length, width);

                        System.out.println("Set Scale: ");
                        newRectangle.setScale(in.nextInt());
                        newRectangle.toString();
                        break;

                case 3: System.out.println("Enter Length: ");
                        length = in.nextFloat();
                        System.out.println("Enter Height: ");
                        height = in.nextFloat();
                        RightTriangle newTriangle = new RightTriangle(length, width);

                        System.out.println("Set Scale: ");
                        newTriangle.setScale(in.nextInt());
                        newTriangle.toString();
                        break;

                case 0: 
                        System.out.println("Exit");
                        break;

                default: System.out.println("Not an a valid option. Please try again.");

            }
        }while(option != 0);
    }
}

Upvotes: 1

Views: 247

Answers (1)

nem035
nem035

Reputation: 35491

Problems:

Remove all invalid modifiers. That is, change:

public float height;
public float width;
public float length;
public float radius;
public float degrees;

To:

float height;
float width;
float length;
float radius;
float degrees; 

Maybe not a direct answer to your question but you need to put your input received option = in.nextInt(); inside the loop. Otherwise you will end up with an infinite loop.

For example, if the user chooses '1', option becomes 1 and is never updated inside the loop so your loop will run forever.

EDIT:

Just to clarify since I see you didn't understand. You don't put int option = in.nextInt(); inside the loop, you put everything but the int. (so option = in.nextInt(); inside the loop). You say int option; outside the loop.

So:

int option;  // created outside the loop
do {
    System.out.print(menu);
    option = in.nextInt();  // used inside the loop
    switch(option)
    {
        // CASES AND CODE
    }
} while(option != 0);

That is why you are getting an error because your variable option isn't visible to the while loop so the loop cannot compare something with 0 if it cannot find it.

In general, try not to create anything inside loops, and create outside of them to prevent unnecessary object creation. Another example, your Scanner object should be created outside of your loop.

Put the statement Scanner in = new Scanner(System.in); outside the loop then use it with option = in.nextInt(); the way I described above.

Upvotes: 3

Related Questions