How to resolve “Syntax error on token “else”” in Java?

The problem is the semicolon after the if.

The grammar is if ( condition ) expression-or-block { else expression-or-block }

Since ; is an empty expression, this works for an if clause and nothing happens in the then clause – in your case syntactically there is no else clause, since your if statement statements ends with the semicolon and is followed by a simple block. Then the tokenizer detects a dangling else token and complains.

Leave a Comment