1

I need to determine which predator in my PredatorList array has received the most damage. For some reason when I try to return that predator, eclipse says - mostDamaged cannot be resolved to a variable.

Why is this so?

public Predator mostDamagedPredator() {
    // test if PredatorList contains predators
    if (PredatorList.length > 0){
        float difference = 0;
        for (int i = 0; i < PredatorList.length; i++) {
            if (PredatorList[i].getMaxHitPoints() - PredatorList[i].getHitPoints() > difference){
                Predator mostDamaged = PredatorList[i];
            }
        }
        return mostDamaged;  // ERROR - mostDamaged cannot be resolved to a variable
    }
    // return null if there are no predators in PredatorList
    return null;
}
2
  • Because there isn't a variable called mostDamaged. Commented Sep 25, 2016 at 10:48
  • Incidentally this can all be written as Arrays.stream(PredatorList).max(Comparator.comparing(p -> p.getMaxHitPoints() - p.getHitPoints())). Further, in Java we use camelCase for variables, PascalCase is reserves for classes - please always stick to this convention. Commented Sep 25, 2016 at 10:49

3 Answers 3

1

You declared mostDamaged inside an if statement block, so it's not within scope outside that block.

Move it outside :

public Predator mostDamagedPredator() {
    if (PredatorList.length > 0){
        float difference = 0;
        Predator mostDamaged = null;
        for (int i = 0; i < PredatorList.length; i++) {
            if (PredatorList[i].getMaxHitPoints() - PredatorList[i].getHitPoints() > difference){
                mostDamaged = PredatorList[i];
            }
        }
        return mostDamaged; 
    }
    return null;
}

or a little better :

public Predator mostDamagedPredator() {
    Predator mostDamaged = null;
    if (PredatorList.length > 0){
        float difference = 0;
        for (int i = 0; i < PredatorList.length; i++) {
            if (PredatorList[i].getMaxHitPoints() - PredatorList[i].getHitPoints() > difference){
                mostDamaged = PredatorList[i];
            }
        }
    }
    return mostDamaged;
}
Sign up to request clarification or add additional context in comments.

2 Comments

I've tried this but my mostDamaged variable in the if statement becomes a duplicate local variable and so when i return mostDamaged, it returns null.
@BobSacamano Please note you don't need to re-declare mostDamaged inside the if statement, just assign to it. See the code in my answer.
1

You have declared mostDamaged variable inside for context.

Declare it out, and initialize it there:

public Predator mostDamagedPredator() {
// test if PredatorList contains predators
if (PredatorList.length > 0){
    float difference = 0;
    Predator mostDamaged = null;
    for (int i = 0; i < PredatorList.length; i++) {
        if (PredatorList[i].getMaxHitPoints() - PredatorList[i].getHitPoints() > difference){
            mostDamaged = PredatorList[i];
        }
    }
    return mostDamaged;  // ERROR - mostDamaged cannot be resolved to a variable
}
// return null if there are no predators in PredatorList
return null;

}

Comments

0

This is because mostDamaged is defined within the if statement of your for loop. This means where you want to return it the variable is not defined.

You can rewrite the method like this:

public Predator mostDamagedPredator() {
    // test if PredatorList contains predators
    Predator mostDamaged = null; // initialize it with null
    if (PredatorList.length > 0){
        float difference = 0;
        for (int i = 0; i < PredatorList.length; i++) {
            if (PredatorList[i].getMaxHitPoints() - PredatorList[i].getHitPoints() > difference){
                mostDamaged = PredatorList[i]; // assign the correct item from the array
            }
        }
    }
    // either mostDamaged was initialized in the if statement or it is still null
    return mostDamaged;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.