• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

Missing return statement...Java

hans030390

Diamond Member
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.
 
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.
 
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.
 
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.

 
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. 😛

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.

 
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.
 
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?

 
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.
 
Back
Top