Dimitar
Dimitar

Reputation: 4783

Missing return Statement of multiple IFs

Where is the problem? If I use a variable it works just fine, but I am missing something.

public boolean xyzThere(String str) {
  if (str.length() > 2){
    if(str.startsWith("xyz")){
        return true;
    } else {
        for (int i = 1; i < str.length() - 2; i++){
            if(str.substring(i, i + 3).equals("xyz") && !str.substring(i - 1, i).equals(".")) {
                return true;
            } else {
                return false;
            }
        }
    }
  } else {
  return false;
  }
}

Upvotes: 0

Views: 529

Answers (4)

Dimitar
Dimitar

Reputation: 4783

Now it is compiling:

public boolean xyzThere(String str) {
  if (str.length() < 3){
  return false;
  } else {
    if(str.startsWith("xyz")){
        return true;
    } else {
        for (int i = 1; i < str.length() - 2; i++){
            if(str.substring(i, i + 3).equals("xyz") && !str.substring(i - 1, i).equals(".")) {
                return true;
            } else {
            }
        }
        return false;
    } 
  }
}

Upvotes: 0

rgettman
rgettman

Reputation: 178263

The Java compiler will not evaluate the conditions to determine if one of if condition and an else if condition will be guaranteed to run.

Without the conditions, the compiler see the possibility of neither condition being true, and in that case, nothing is returned. There are not return statements past the else if. This occurs even if we see that logically one of them will be true.

Just turn your else if into an else to satisfy the compiler.

else {
    return false;
}

Now the compiler will see that there is a return statement in all possible execution paths.

In these cases, sometimes I comment out the condition to let readers know the real intent.

else // if (str.length() < 3)
{
    return false;
}

Update:

Something similar is occurring inside the for loop. The compiler won't assume that at least one iteration of a for loop will occur. If the for loop body is never entered, then the else block ends, and the body of the outer if block ends without a return statement`.

You must provide a return statement after the for loop, in case the for loop is never entered.

Upvotes: 0

Neeraj Jain
Neeraj Jain

Reputation: 7730

Because it might be possible that none of your outer if condition succeed , So Compiler is making sure that your program doesn't get stuck into the function , by throwing the missing return statement error

So try this way :

if(condition){
return false;
}
elseif(condition){
return true;
}
return false; <--- Now compiler is assured that function will return something 

Assuming the return type is boolean

Upvotes: 0

minion
minion

Reputation: 4353

This condition needs a return statement as the code inside for loop may not be reachable.

else {
                for (int i = 1; i < str.length() - 2; i++) {
                    if (str.substring(i, i + 3).equals("xyz") && !str.substring(i - 1, i).equals(".")) {
                        return true;
                    } else {
                        return false;
                    }
                }
            }

Upvotes: 1

Related Questions