Chaps, I am having some problems with a return statement in a function.
I have this situation:

    public boolean isFine(){
        for( int i = 0; i < myarray.length; i++ ){
            for(int j = 0; j < myarray[i].length; j++ ){
                if( myarray[i][j] == myEnumeration.FRIDAY ){
                    return false;
                }
                else{
                    return true;
                }
            }//end of column array
        }//end of row array     
    }//end of isFine()

I am returning values to the caller using if statements. Now, the compiler is telling me that a return statement is missing, but in fact I have 2. Does it say that because it wants me to have a return statement further down, before the last set of brackets? If so how can I possibly do it?!
thanks

Recommended Answers

All 6 Replies

Basically, what it comes down to is that you have both of the return clauses inside of an if/else statement, and the compiler does not have the ability to determine for certain that at least one of the options is guaranteed to run.

The solution in this case is simple: get rid of the else clause, and move the second return clause to the end, after the last of the for loops exits.

    public boolean isFine(){
        for( int i = 0; i < myarray.length; i++ ){
            for(int j = 0; j < myarray[i].length; j++ ){
                if( myarray[i][j] == myEnumeration.FRIDAY ){
                    return false;
                }
            }//end of column array
        }//end of row array 
        return true;
    }//end of isFine()

This is actually necessary anyway; with the original version, the function would have exited on the first pass of the inner loop one way or the other, when what you presumably wanted was for it to return true if no cases proved to equal myEnumeration.FRIDAY.

Actually, there is no problem with having only one return statement in the if part and one in the else part. The compiler is surely smart enough to recognize that one return or the other is inevitable. The problem comes from the fact that it is possible for myarray to have zero-length, and also for every element of myarray to be a zero-length array, meaning the loops get skipped without running even once.

Even if that can never happen, the compiler wouldn't even try to detect that because you would be better off making a decision about what the method should do in that case. The compiler is suggesting a return value, but you could also throw an exception

If you think that it is impossible for the method to run without encountering either of the return statements then you should throw an Error at the end of the method, because Errors are what get thrown when the most serious of unrecoverable problems happen, and things which aren't possible are also usually impossible to recover from.

but myarray having a 0 length is not really "impossible", so throwing an error or exception, might not be the best idea.

thanks for your replies,
@Schol-R-LEA:

when what you presumably wanted was for it to return true if no cases proved to equal myEnumeration.FRIDAY

That is correct, it's exactly what I want to do, I didn't realize that in the way I construct the function it would exit at the first pass, so I will change it as you suggested.

@bguild:

The problem comes from the fact that it is possible for myarray to have zero-length,

I appreciate that my snippet was only a function so it doesn't say much about the rest of the program, but I would have thought that the array always has a lenght > 0 because I initialize it, so I think it will never be empty
thanks

Violet: the compiler looks at only the method at hand. what if you set your array using a setter or in another method to be 'new MyArray[0][0];'? the compiler doesn't/can't know about that.

also, bit an update on your terminology :) Java doesn't have functions, it has methods

ok fair enough, thanks for clarifying that.
Yes I get confused sometimes between methods and functions, that's what happen when you do javascript as well!
thanks

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.