2
public int checkGuess(int g, int randomnumber){

    number=g;
    this.randomnumber= randomnumber;

    if (number == randomnumber)
        return 0;

    else if (number < randomnumber)
        return -1;

    else if (number > randomnumber)
        return 1;

}

why is this giving me a missing return statment error? every if/else has a return the error comes up for the last bracket

2
  • 2
    a return should be made inside an else or outside the entire if/else-ifs Commented Nov 27, 2013 at 1:38
  • Because you're missing a return statement for the implied else. Commented Dec 2, 2013 at 22:37

8 Answers 8

12

Every return statement is inside an if statement. While it may be logically impossible as written, the compiler needs a return for when none of the if evaluate true.

I recommend:

public int checkGuess(int number, int randomnumber){
    int retVal = 0;
    this.randomnumber= randomnumber;

    if (number == randomnumber) {
        retVal = 0;
    } else if (number < randomnumber) {
        retVal = -1;
    } else if (number > randomnumber) {
        retVal = 1;
    }
    return retVal;
}

This solution fixes the compiler problem and improves readability slightly, in my opinion.


Alternatively, there's this solution:

public int checkGuess(int number, int randomnumber){
    this.randomnumber= randomnumber;

    if (number == randomnumber) {
        return 0;
    } else if (number < randomnumber) {
        return -1;
    } else if (number > randomnumber) {
        return 1;
    } else {
        //throw an exception
    }
}    

Throwing an exception will allow you to get out of the method without returning anything... because arguably, if you get to the final else, something clearly went wrong.

Sign up to request clarification or add additional context in comments.

5 Comments

+1 for suggesting throwing an exception, which is really the best thing to do when you're that confident the code is unreachable.
+1 for me throwing IllegalArgumentException is the best if you think the method should not entertain value that will not satisfy one of the defined conditions.
Just because the compiler can't figure out that there are ONLY three posssibilities doesn't mean that the programmer can't
And regardless of whether or not the programmer has figured this out, the compiler is the one that matters... he's the one that compiles your code.
A sneaky little point in the first solution--which does generally seem nice--is: int retVal = 0; sneaks a little initial value into this variable. Do you like this initial value? Do you want it? Does it make sense? Maybe you don't want it. Well delete that initial value, and, presto!, you will have replaced your Missing return statement error with a Variable might not have been initialized error. They get you coming and going... (You can of course fix not initialized by... adding a bare else for retVal...) Ok, we also admit that we know that the ifs exhaustively overwrite initial
6

You could try changing the las else if to else.

if (number == randomnumber)
    return 0;

else if (number < randomnumber)
    return -1;

else
    return 1;

1 Comment

yeah i know that works, but i think not saying that if number is greater return 1; messes up the tester file. any way to still have that condition?
2

The compiler is not required to be able to figure out whether or not your if/else tree covers every possible case. And it would be awful if you could write code that some compilers were smart enough to figure out were okay and other compilers weren't. Rather than having a precise specification for exactly how smart a compiler has to be, Java requires you to write clear, clean code.

The closing curly brace of the function is, by the definition in the Java specification, reachable. That you can prove that it is not reachable by some other definition of "reachable" doesn't matter. The Java specification notion of "reachable" is a formal notion explained in detail in the specification that compilers can actually implement. It is not the common sense notion of "reachable", which one could never teach to a computer anyway.

Comments

2

Basically, the compiler is not smart enough to realzie that number == randomnumber || number < randomnumber || number > randomnumber is a tautology. Since you don't have an else, it thinks it's possible you will get past the conditional and then hit the end of function without returning. The easiest fix is to change your last elseif to just an else. You know from the previous conditions that number > randomnumber must be true if you get to that point. (You could add an assert if you're paranoid and want to be sure.)

Comments

2

There is no need for an else after a return statement:

if (number == randomnumber)
  return 0;

if (number < randomnumber)
  return -1;

// Must be true: if (number > randomnumber)
return 1;

Note that the reason this solves the problem is because the compiler does not check for solutions that logically must return. e.g. while A must be > < or = to B, it does not check for that relationship. It is looking for possibilities that cover every path explicitly.

3 Comments

While the else following returns isn't necessary, it helps for readability (especially in cases where the if body is more that a simple return 0;. But either way, that part is irrelevant to the actual problem (though your posted code does solve the problem...just doesn't explain the problem, leaving a misleading answer).
@nhgrif Added Note to explain why, also {} make this clearer, with or without the else. I didn't add them to highlight the difference I was presenting.
@nhgrif I believe the code is more readable and logical when ELSE is not used after IF-return statement.
1

a return should be made inside an else or outside the entire if/else-if. It's possible a return is never made with all returns depending on a condition

if (condition 1)
    return a;
else if (condition 2)
    return b;
else if (condition 3)
    return c;

If neither condition 1, 2, or 3 are met there will no return. A return should always be made available. So a fix would be:

if (condition 1)
    return a;
else if (condition 2)
    return b;
else if (condition 3)
    return c;
else
    return d;

Or

if (condition 1)
    return a;
else if (condition 2)
    return b;
else if (condition 3)
    return c

return d;

Comments

0

You need an else for that to work as you expect, it does not evaluate your conditions to know that it will always return

Comments

0

Java requires that non-void methods are guaranteed to return something (even if it's null). If there is an if statement, by definition, only sometimes your return statements are run.

if(number == randomnumber) {
    return 0;
} else if(number < randomnumber) {
    return -1;
} else if(number > randomnumber) {
    return 1;
} else {
    return -2;
}

The else statement isn't required, but it does make it easier to understand imo.

1 Comment

else return -2; is pretty sloppy here, in my opinion and hinders readability. This answer does solve the compiler problem though.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.