How to avoid "unreachable statement" and "else without if" in Java?

3.2k Views Asked by At

So my code is meant to compare the y co-ordinate of the robot with the y co-ordinate of the target. I'm having problems making the function return anything when I add the print statements in. I have a feeling this has something to do with brackets, but I'm not exactly sure how to use them.

This is not the entire program, but it's the only bit with errors in it.

When I try to compile this:

public class Ex12 
{

    private byte isTargetNorth(IRobot robot)
    {

        if (robot.getLocationY() > robot.getTargetLocation().y) {

            return 1; System.out.println("north");

        } else if (robot.getLocationY() == robot.getTargetLocation().y) {

            return 0; System.out.println("no");

        } else { 

            return -1; System.out.println("south");

        }  

    }
}

I get error: unreachable statement

When I try this:

public class Ex12 
{

    private byte isTargetNorth(IRobot robot)
    {

        if (robot.getLocationY() > robot.getTargetLocation().y)

            return 1; System.out.println("north");

        else if (robot.getLocationY() == robot.getTargetLocation().y)

            return 0; System.out.println("no");

        else

            return -1; System.out.println("south");

    }
}

I get error: 'else' without 'if'

I get no errors when I remove the System.out.println() functions.

4

There are 4 best solutions below

0
On

The return statements exit your method. So, the System.out's will never be called.

0
On

The first one: Move the return statements after their respective System.out.println calls - return exits the current method, so System.out.println never gets called and is thus unreachable.

The second one is a case of confusing formatting: your code

if (robot.getLocationY() > robot.getTargetLocation().y)
    return 1; System.out.println("north");
else if ...
//...

Is actually equivalent to

if (robot.getLocationY() > robot.getTargetLocation().y) {
    return 1;
}
System.out.println("north");
else if ... //else without if right here, because else should follow immediately after an if block!

The else without if example is a good reminder of why you should be extra careful when omitting braces.

0
On

In your first code block you have System.out.println after the return statement which makes them unreachable. If you put System.out.println in front of the return statements it'll work.

In your second example your removed the bocks ({...}) from the if statements which means you can only have exactly one statement per condition. You have two return and System.out.println.

0
On

You wrote:

return 1;
System.out.println(""); // << This is written after you return to the calling method.

This means, you cannot write code here.

In the other code you wrote

if() Some Code;
// This statement will be executed, because  it is outside of the if-block
else 
    // The else above has no if.

To solve your problems:

  1. Never write code after you return the return value.
  2. Write parenthesis {} Mor to write, but you can always see the blocks.