Search code examples
javafor-loopbluej

for-each loop, attempting to use equalsIgnoreCase() method


First question of mine:

Currently I have an assignment in BlueJ where I need to apply for. each loops. My code is as follows:

my constructor:

if(isValidPopulation(populationInMillions) && isValidStateProvinceName(provinceStateName) && isValidCapitalCity(provinceStateCapital)) {
         this.populationInMillions   = populationInMillions;
         this.provinceStateName      = provinceStateName;
         this.provinceStateCapital   = provinceStateCapital;
}

one of the methods that helps with my constructor:

private boolean isValidStateProvinceName(String validName)
{
    for(String temp : validStateProvince)
    {
        if(validStateProvince.contains(temp.equalsIgnoresCase(validName))) {
            return true;
        } else {
            return false;
        }
    }
}

Basically I wish to make is so that validName can use any cases in the loop statement. Obviously I am executing this incorrectly. I will provide more code if anyone could be so kind to help.


Solution

  • I think this is what you are looking for:

    private boolean isValidStateProvinceName(String validName)
    {
        for(String temp : validStateProvince)
        {
            if(temp.equalsIgnoresCase(validName)) {
                return true;
            }
        }
        return false;
    }
    

    Also, since you are iterating throug validStateProvince, you want to compare it to the temp.