-2

I have a function defined that is designed to return either true or false value but the variable result is not being updated by the inner ifs though they are being executed.

What concept of scoping am I unable to get here?

    public boolean isEligible(String id) {
    boolean result;
    for(Person person : p) {
        if(person.getId() == id) {
            if(person.isRecovered() || person.getNumDoses() == 2) {
                result = true;
            }
        }else {
            result = false;
        }
        
    
}
    return result;

}

This is showing me an error: "the variable result might not have been initialized" After initializing it with false value the variable is returning me false for all iterations.

Please help me to clear the concept and provide the solution.

user207421
  • 305,947
  • 44
  • 307
  • 483
  • 1
    you need to initialize local variable result – sanjeevRm Sep 16 '21 at 07:58
  • 1
    This is two problems, really. The error is easy to explain: what would you expect to be returned if `p` is empty, or if it contains a single person with a matching ID, but who isn't recovered and has a number of doses other than 2? – Jon Skeet Sep 16 '21 at 07:58
  • Does this answer your question? [How do I compare strings in Java?](https://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) – luk2302 Sep 16 '21 at 07:58
  • 1
    What happens if `p` is empty? – MC Emperor Sep 16 '21 at 07:59
  • 3
    As for your second issue: if the very last person doesn't have a matching ID, you'll set result to false even if it had previously been set to true. Fixing this is really easy: just get rid of the local variable entirely, return true if you find any matches (immediately), and return false if you get to the end of the loop. – Jon Skeet Sep 16 '21 at 07:59
  • 1
    instead of assigning value to local variable, you can directly return true or false – sanjeevRm Sep 16 '21 at 08:02

1 Answers1

1

There are several issues here:

Firstly, the compiler is correct: the variable result might not have been initialized. For example, p might be empty.

Secondly, your ID comparison uses == between two strings. You should use equals, as explained in this question.

Thirdly, if the loop finds an eligible person but any later person doesn't match by ID, it will set the result to false.

The simplest fix to the first and last issues is to get rid of the result variable entirely, and just return as soon as you know the result. Assuming that any given ID only appears once in the list, you can do that as soon as you've found a matching entry. If you don't find any matching entries, return false.

public boolean isEligible(String id) {
    for (Person person : p) {
        // Assumption 1: person.getId() is never null.
        // Assumption 2: the same ID never occurs twice in the list
        // (i.e. once we've found a matching person, we know the result)
        if (person.getId().equals(id)) {
            return person.isRecovered() || person.getNumDoses() == 2;
        }
    }
    return false;
}
Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194