views:

179

answers:

8
import java.util.Scanner;

public class SW {

/**
 * @param args
 */
public static void main(String[] args) {
 int length;
 int width;
 int number;
 int perimeter;
 int area;
 Scanner input = new Scanner( System.in ); 
 System.out.println("1. Dimensions of the rectangle");
 System.out.println("2. Perimeter of the rectangle");
 System.out.println("3. Area of the rectangle");
 System.out.println("4. Check if the rectangle is a square");
 System.out.println("Enter the number you want to check");
 number=input.nextInt();
 System.out.println("Enter the length of the rectangle");
 length=input.nextInt();
 System.out.println("Enter the width of the rectangle");
 width=input.nextInt();
 switch (number){
 case 1:
  System.out.println("The dimensions of the rectangle are-");
  System.out.println("Length-"+length);
  System.out.println("Widht-"+width);
  break;
 case 2:
  System.out.println("The perimeter of the rectangle is-"+2*(length+width));
  break;
 case 3:
  System.out.println("The area of the rectangle is-"+length*width);
  break;
 case 4:
  if (length==width);
  System.out.println("The rectangle is a square");
  **else** if(length!=width) {System.out.println("The rectangle is not a square");}
  break;
  }

 // TODO Auto-generated method stub

}

}
A: 

Well, you don't say what syntax error you're getting and what line.

The only thing I can see is that your "else" in case 4 is surrounded by double asterisks.

Ah - reading your title again - you may be referring to just that. The "else". Well it's invalid to surround a keyword with double asterisks in Java.

Were you meant to comment it out? If so, use /* else */ .

Wim Hollebrandse
There's also an extra } at the end of the file?
marcc
That's the end of the switch.
Wim Hollebrandse
Hmm. Kinda realised now that the OP put those there for emphasizing where the problem was. Duh.
Wim Hollebrandse
+10  A: 

You have a stray semicolon terminating your "if":

        case 4:
            if (length==width);   // remove the semicolon and add curly braces
            System.out.println("The rectangle is a square");
            **else** if(length!=width) {System.out.println("The rectangle is not a square");}
            break;
            }
shoover
A: 

The semi-colon:

if (length==width);

It should be:

if (length==width)
James Black
+3  A: 

if (length==width);

I think that line is your culprit. Or, more specifically the ";" at the end of it. That's closing out the if statement, which means the else that comes later is dangling.

saleemshafi
Good point, had overlooked that. But the asterisks around the else look funny too. Not sure if that's SO formatting's engine though.
Wim Hollebrandse
A: 

Others have already pointed out the semicolon which is causing the problem.

if (length==width)
    System.out.println("The rectangle is a square");
else if(length!=width) {
    System.out.println("The rectangle is not a square");}
    break;
}

There's some superfluous code, even once the semicolon is gone. Since length!=width is the opposite of length==width, you can simply use else, thus:

if (length==width)
    System.out.println("The rectangle is a square");
else {
    System.out.println("The rectangle is not a square");}
    break;
}
Carl Manaster
I think the `break` should be outside the `if` statement, not part of the `else` clause. And putting braces around the 'else' part but not the 'then' part is confusing.
finnw
+2  A: 

Now that everyone has pointed out the syntax errors, I'd like to suggest that you read Sun's Java Style Guide. Among other things, it recommends that:

  • there should be 1 space before and after any infix operator; e.g. + 2 * (length + width) not +2*(length+width)

  • there should be 1 space before and after the "=" in an assignment; e.g. width = input.nextInt(); not width=input.nextInt();

  • there should NOT be a space around the argument list in a method or constructor invocation; e.g. new Scanner(System.in); not new Scanner( System.in );

  • and so on.

Now some people think that style doesn't matter. But if you are working in a team ... or simply reading someone else's code ... you will find that good (or at least consistent) style is very important in making your code understandable to other people ... and even to yourself at 2am when you are trying to track down some critical bug.

So even if you are just starting out programming you should be learning to get the style right in your code. (And I don't care if you are still at school and your lecturers and TA's don't think teaching good coding style is important. They are WRONG.)

Stephen C
finnw
@finnw - I deliberately created this answer as Community Wiki so that people could expand and correct it. Hint. Hint.
Stephen C
A: 

Because of this:

if (length==width);<------

The semicolon ends the if and then you have a floating else which is indeed invalid syntax.

OscarRyz
+1  A: 

You are using Eclipse. Here the whole source code for your class can be reformatted with Ctrl-A followed by Ctrl-Alt-F.

This will make your semicolon error very clear as the line below is not indented.

Thorbjørn Ravn Andersen