DrinkJavaCodeJava
DrinkJavaCodeJava

Reputation: 808

Switch statement for some reason does not like case 8. Compiler is acting real weird

Here is the code:

public static void main(String args[])
    {
        int i=0;
        int m=0;
        double scale;
        boolean exit;
        Shape[] s = new Shape[10];
        while(exit !=true)
        {
            System.out.print("\nChoose an option:\n"+
                    "1-Add a new circle\n"+
                    "2-Add a new rectangle\n"+
                    "3-Delete all shapes\n"+
                    "4-Scale all shapes\n"+
                    "5-Display perimeter of all shapes\n"+
                    "6-Display the area of all shapes\n"+
                    "7-Enter scale factor\n"+
                    "8-Exit program\n");

            Scanner input = new Scanner(System.in);
            m=input.nextInt();


            if(i<=9)
            {
                switch (m)
                {


                 case 1: Circle c = new Circle(0);
                         s[i]=c;
                         i++;
                         break;
                 case 2: Rectangle r = new Rectangle(1,1);
                         s[i]=r;
                         i++;
                         break;

                 case 3: s=null;
                         i=0;
                         break;
                 case 4: for(i=0; i<s.length; i++)
                         {
                             s[i].scaleShape();
                         }
                         break;
                 case 5: for(i=0; i<s.length; i++)
                         {
                            if(s[i] != null)
                            {   
                                System.out.println(s[i].getPerimeter());
                            }
                         }
                         break;
                 case 6: for(i=0; i<s.length; i++)
                         {
                            if(s[i] != null)
                            {
                                System.out.println(s[i].getArea());
                            }
                         }
                         break;
                 case 7: do
                         {
                            System.out.println("\nEnter scale factor");
                            scale=input.nextDouble();
                         }
                         while(scale<0);
                         Shape.setScaleFactor(scale);

                         }
                         break;

                 case 8: System.out.println("Do you want to quit?");
                         break; //Nothing here since loop should terminate it.



                 //default: System.out.println("Number must be 1-8");
                         // break;


              } 

         }
     }
   }

Oddly the compiler is giving me an error on case 8 saying:

Type mismatch can't convert from int to boolean.

But Im not converting anything to boolean

-syntax error on token "case" assert expected -syntax error on token :, ; expected

But all the commands there have semi-colons

expression must return a value

Why is the compiler acting so funny? Normally errors like that are easy to find. What is going on?

Upvotes: 0

Views: 5113

Answers (4)

MadProgrammer
MadProgrammer

Reputation: 347184

Every one has pointed out that you have an extra parenthesis in your code, what we've failed to point out is where it's coming from...

while(scale<0); // <-- This isn't going to work....
    Shape.setScaleFactor(scale);
}

It should be...

while(scale<0) {
    Shape.setScaleFactor(scale);
}

The next question is, how is scale decremented? Cause this could cause an infinite loop if you're not careful.

Upvotes: 1

Mudassir Hasan
Mudassir Hasan

Reputation: 28741

while(scale<0);
Shape.setScaleFactor(scale);

} // Remove this parenthesis.
break;

Upvotes: 0

pb2q
pb2q

Reputation: 59607

Your problem is in the case for 7:

case 7: do
        {
             System.out.println("\nEnter scale factor");
             scale=input.nextDouble();
        }
        while(scale<0);
        Shape.setScaleFactor(scale);

        }

Notice the extra close brace: that's closing your switch statement, orphaning your case 8.

Upvotes: 5

Brendan Long
Brendan Long

Reputation: 54242

                     } // <-- Why is this here?
                     break;

             case 8: System.out.println("Do you want to quit?");

You're ending the switch statement with an extra }. Remove it and things may work.

Upvotes: 4

Related Questions