user3670333
user3670333

Reputation: 75

Compiler says missing return statement but I already have 3

This is just weird. My compiler says I am missing a return statement, but I already have 3. Here is my code:

public int tortoiseMoves()
{
    int i = tGen();
    if (i >= 1 && i <= 5)
    {
        int fastplod = 3;
        return fastplod;
    }

    if (i >= 6 && i <= 8)
    {
        int slowplod = 1;
        return slowplod;
    }

    if (i >= 9 && i <= 10)
    {
        int slip = -6;
        return slip;
    }
}

Upvotes: 3

Views: 904

Answers (7)

bcorso
bcorso

Reputation: 47166

Only one of your if(...) statements can be true for a given run of the function. Therefore, it will probably be more clear to use if/if else/else structure. You could put the return or throw statement outside the if structure, but I think putting it in the else makes it more clear.

public int tortoiseMoves() {
    int i = tGen();                         // random: 1 <= i <= 10
    if      (i >= 1 && i <= 5)  return 3;   // fastplod
    else if (i >= 6 && i <= 8)  return 1;   // slowplod
    else if (i >= 9 && i <= 10) return -6;  // slip
    else throw new IllegalStateException(); // this shouldn't happen!
}

Upvotes: 2

bcorso
bcorso

Reputation: 47166

Warning: many of the comments suggest that your error is due to not telling the compiler what to do for ALL conditions. However, it's important to realize that even if you cover ALL possible conditions with if/if else the compiler will still complain.

public boolean isPositive(int x){
    if(x > 0)        return true;
    else if(x <= 0)  return false;
    // error: "This method must return a result of type boolean"
}

In this case the compiler technically has enough information to determine that one of the above statements must be true because x ∈ ℤ = {x | x ≤ 0} ⋃ {x | x > 0}. However, you'll still need the last return either inside an else or outside the if structure.

Upvotes: 1

Federico Santamorena
Federico Santamorena

Reputation: 499

Simple, if all the if statements are false the code doesn't know what to return!

I don't know if this is a better solution for your case:

public int tortoiseMoves()
{
    switch (tGen())
    {
        case 1:
        case 2:
        case 3:
        case 4:
        case 5: return 3; // slowplod
        case 6:
        case 7:
        case 8: return 1; // fastplod
        case 9:
        case 10: return -6; // slip
        default: return 0; // default value if not in range
    }
}

Solution 2 (lookup table): //fastest without using switch or branching with if

public int tortoiseMoves()
{
    int[] moves = {3,3,3,3,3,1,1,1,-6,-6};
    return moves[tGen()-1];
}

Be careful of ArrayOutOfBondException!

Upvotes: 2

slevin
slevin

Reputation: 318

Two ways to avoid this problem in general:

The default return:

public int tortoiseMoves()
{
    int i = tGen();
    if (i >= 1 && i <= 5)
    {
        int fastplod = 3;
        return fastplod;
    }

    if (i >= 6 && i <= 8)
    {
        int slowplod = 1;
        return slowplod;
    }

    if (i >= 9 && i <= 10)
    {
        int slip = -6;
        return slip;
    }

    //If code reaches here, you know none of the other returns were called
    return someDefaultValue;
    // OR
    // throw new Exception("Invalid value generated by tGen(): " + i);

}

Or the if/else

public int tortoiseMoves()
{
    int i = tGen();
    if (i >= 1 && i <= 5)
    {
        int fastplod = 3;
        return fastplod;
    }
    else if (i >= 6 && i <= 8)
    {
        int slowplod = 1;
        return slowplod;
    }
    else //Code is guaranteed to reach here and return if the previous if statements fail
    {
        int slip = -6;
        return slip;
        // OR
        // throw new Exception("Invalid value generated by tGen(): " + i);
    }
}

Upvotes: 3

user432
user432

Reputation: 3534

You have to make sure that there is always a value returned. If all your conditions fail you don't return anything.

A fix would be to chain your if statements because they are exclusive and use a else to catch all other cases.

public int tortoiseMoves() {
    int i = tGen();
    if (i >= 1 && i <= 5)
    {
        int fastplod = 3;
        return fastplod;
    }

    else if (i >= 6 && i <= 8)
    {
        int slowplod = 1;
        return slowplod;
    }

    else if (i >= 9 && i <= 10)
    {
        int slip = -6;
        return slip;
    }
    else {
        // return something or throw exception
        return 0;
    }
}

Upvotes: 8

Alejandro Alcalde
Alejandro Alcalde

Reputation: 6220

You need to add a return statement for every possible case in the code. It can be a case in which all the ifs evaluate to false and hence, no return statement will execute. Try this:

public int tortoiseMoves()
{
    int i = tGen();
    if (i >= 1 && i <= 5)
    {
        int fastplod = 3;
        return fastplod;
    }

    if (i >= 6 && i <= 8)
    {
        int slowplod = 1;
        return slowplod;
    }

    if (i >= 9 && i <= 10)
    {
        int slip = -6;
        return slip;
    }

    return Something to return if none of the ifs is true
}

Althought you can change the code to this:

public int tortoiseMoves()
{
    int i = tGen();
    if (i >= 1 && i <= 5)
    {
        int fastplod = 3;
        return fastplod;
    } else if (i >= 6 && i <= 8){
        int slowplod = 1;
        return slowplod;
    } else if (i >= 9 && i <= 10) {
        int slip = -6;
        return slip;
    } else {
        return Something if none of the ifs is true
    }
}

Upvotes: 4

wastl
wastl

Reputation: 2641

There has to be at least one return statemant that will surley be reached, but there is no such statement in your code

Upvotes: 7

Related Questions