Missing return statement...Java

hans030390

Diamond Member
Feb 3, 2005
7,326
2
76
I'm trying to write code that takes three integers and returns the median of those values. Here is what I have so far (I think it's correct, though it may not be):

public static int median(int x, int y, int z) {
if (x > y && x > z)
if (y > z)
return y;
else
return z;
if (x > y && x < z)
return x;
if (x < y && x < z)
if (y > z)
return z;
else
return y;

}

When compiling, I keep getting one error: Missing return statement {

I'm still new to Java, so I'm not sure what I'm missing here.
 

degibson

Golden Member
Mar 21, 2008
1,389
0
0
public static int median(int x, int y, int z)
if (x < y && y < z) return y;
if( x < z && z < y) reutnn z;
return x;
}

FWIW, your "if (x < y && x < z)" could not be true, and therefore reach the end of your function.

To make this kind of thing easier to spot, ALWAYS use curly braces on if statements -- even simple ones.
 

hans030390

Diamond Member
Feb 3, 2005
7,326
2
76
Originally posted by: degibson
public static int median(int x, int y, int z)
if (x < y && y < z) return y;
if( x < z && z < y) reutnn z;
return x;
}

FWIW, your "if (x < y && x < z)" could not be true, and therefore reach the end of your function.

To make this kind of thing easier to spot, ALWAYS use curly braces on if statements -- even simple ones.

Ok, so I've obviously over thought the method, but why can't "if (x < y && x < z)" be true? Obviously how you coded it is the better way, but I still don't see why that can't be true.
 

degibson

Golden Member
Mar 21, 2008
1,389
0
0
Originally posted by: hans030390
why can't "if (x < y && x < z)" be true?
(x < y && x < z) could be true. In fact, your implementation is actually correct and the compiler issue is a false positive. Static compiler analysis just checks to see if each basic block is reachable in isolation -- it usually doesn't look at broader invariants between basic blocks.

Edit: I partially take this back. I haven't reasoned though the x==y and y==z cases.

 

Ken g6

Programming Moderator, Elite Member
Moderator
Dec 11, 1999
16,592
4,498
75
Originally posted by: hans030390
if (x > y && x > z)
if (y > z)
return y;
else
return z;

See this? This is a classic programming problem. See, Java doesn't care about indentation; it sees your code kind of like this forum displays it. :p

Anyway, you see you have two if's and one else. To which if does the else belong? I'm not really sure what Java comes up with in this case, but there's a 50/50 chance it's not what you wanted. This is why you should always use curly braces on anything that's more than one line. (Always using curly braces period isn't bad, either.)

That probably doesn't resolve your error, but it's something to be aware of.

 

awal

Senior member
Oct 13, 1999
953
0
0
One approach to finding the median would be to store the int values in a sorted array and return the value at the middle position of the array.
 

blahblah99

Platinum Member
Oct 10, 2000
2,689
0
0
Originally posted by: hans030390
I'm trying to write code that takes three integers and returns the median of those values. Here is what I have so far (I think it's correct, though it may not be):

public static int median(int x, int y, int z) {
if (x > y && x > z)
if (y > z)
return y;
else
return z;
if (x > y && x < z)
return x;
if (x < y && x < z)
if (y > z)
return z;
else
return y;

}

When compiling, I keep getting one error: Missing return statement {

I'm still new to Java, so I'm not sure what I'm missing here.

It's missing a return statement because if none of those conditions are met, nothing is returned. What happens if the program skips through the three main if statements?

 

hans030390

Diamond Member
Feb 3, 2005
7,326
2
76
Originally posted by: awal
One approach to finding the median would be to store the int values in a sorted array and return the value at the middle position of the array.

We hadn't learned arrays at that time.

Originally posted by: blahblah99
It's missing a return statement because if none of those conditions are met, nothing is returned. What happens if the program skips through the three main if statements?

I got it figured out, but I know what you're saying.