Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

if statement OR logic oddity

Tags:

java

android

I'm calling this function from a jUnit test case with the following information:

// abbr = "US";
// Countries  = array of two objects one with iso2 = "us"
public Country getCountryFromAbbr(String abbr) {
    abbr = abbr.toLowerCase();
    for (int i = 0; i < Countries.size(); i++) {
        Country country = Countries.get(i);
        String iso2 = country.ISO2.toLowerCase();
        String iso3 = country.ISO3.toLowerCase();

        if (iso2.equals(abbr) || iso3.equals(abbr)) {
            return country;
        }
    }

    return null;
}

When I debug, the second object with ISO2 of us iso2.equals(abbr) is true and the other is false. However, country is not returned and the debugger finishes the loop and returns null.

I'm confused as true || false is true. Am I missing something?

Here's the mock of the countries:

    List<Country> Countries = new ArrayList<Country>();
    Country country = new Country();
    country.CountryId = 1;
    country.CountryName = "Great Britian";
    country.ISO2 = "GB";
    country.ISO3 = "GBR";
    Countries.add(country);

    Country usa = new Country();
    usa.CountryId = Studio.USA_COUNTRY_ID;
    usa.CountryName = "United States of America";
    usa.ISO2 = "US";
    usa.ISO3 = "USA";
    Countries.add(usa);
    return Countries;


EDIT: I'm using Eclipse and debugging using my Droid X 2.3.3
like image 604
Joe Avatar asked Apr 12 '26 10:04

Joe


1 Answers

Does it work fine with simple if condition?

if (iso2.equals(abbr)) {
            return country;
        }
if(iso3.equals(abbr)){
       return country;
}
like image 109
Lalit Poptani Avatar answered Apr 14 '26 23:04

Lalit Poptani



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!