Search code examples
javabluej

Unreachable Statement Error Message


I am making a simple program. When I compile it, BlueJ says "unreachable statement" and highlights "System.out.println(mystery("DELIVER"));". I'm unsure what to do. I would like it to complete the println statement and run it in a main method, but it gives me the same error.

public class Test7
{
    public String mystery(String s)
    {
        String s1= s.substring(0,1);
        String s2= s.substring(1, s.length()-1);
        String s3= s.substring(s.length()-1);

        if (s.length() <= 3)
            return s3 + s2 + s1;
        else 
            return s1 + mystery(s2) + s3;
        System.out.println(mystery("DELIVER"));
    }
}

Solution

  • Whether the if-statement is true or false, you return, so the println statement cannot be reached.

    Maybe that became clearer once I corrected your bad indentations.

    Cannot answer what you should do, since you haven't declared your intention. Did you intend the println statement to be in a separate main method, perhaps?