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

Stock_Clue
  • 35
  • 1
  • 1
  • 4

8 Answers8

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.

nhgrif
  • 61,578
  • 25
  • 134
  • 173
  • 1
    +1 for suggesting throwing an exception, which is really the best thing to do when you're that confident the code is unreachable. – Flight Odyssey Nov 27 '13 at 03:23
  • +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. – Bnrdo Nov 27 '13 at 04:06
  • Just because the compiler can't figure out that there are ONLY three posssibilities doesn't mean that the programmer can't – Glenn Teitelbaum Dec 02 '13 at 01:51
  • 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. – nhgrif Dec 02 '13 at 06:18
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;
Christian Tapia
  • 33,620
  • 7
  • 56
  • 73
  • 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? – Stock_Clue Nov 27 '13 at 01:40
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.

David Schwartz
  • 179,497
  • 17
  • 214
  • 278
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.)

Dave Lillethun
  • 2,978
  • 3
  • 20
  • 24
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.

MaxZoom
  • 7,619
  • 5
  • 28
  • 44
Glenn Teitelbaum
  • 10,108
  • 3
  • 36
  • 80
  • 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 Dec 02 '13 at 20:52
  • @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. – Glenn Teitelbaum Dec 02 '13 at 22:07
  • @nhgrif I believe the code is more readable and logical when ELSE is not used after IF-`return` statement. – MaxZoom Nov 12 '15 at 21:54
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;
Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720
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

aw04
  • 10,857
  • 10
  • 56
  • 89
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.

Andrew Gies
  • 719
  • 8
  • 20
  • `else return -2;` is pretty sloppy here, in my opinion and hinders readability. This answer does solve the compiler problem though. – nhgrif Dec 02 '13 at 20:55