user1556464
user1556464

Reputation: 47

For Loop not incrementing with nested IF Loop

I have a for loop that for the life of me I cant figure out why it isnt incrementing properly. I know that it is due to the 'If Statement' that is nested in the first For Loop but I have no idea how to fix it. Any help would be greatly apreciated.

public static boolean ifMatches(String word){
    String[] split = word.split("z");
    for(int i = 0; i<split.length; i++){
        if(vowelCount(split[i]) == 2){
            return true;
        }else{
            return false;
        }       
    }
    return false;
}


public static int vowelCount(String part){
  int vowelCounter = 0;
    for( int i = 0; i <part.length(); i++){
       if(isVowel(part.charAt(i)))
        vowelCounter++;
    }
    return vowelCounter;

Upvotes: 0

Views: 212

Answers (1)

Bart W
Bart W

Reputation: 137

The loop is not incrementing because you return a value in the if statement and the else statement; there is no way for the for loop to go through more than one iteration because it always returns a value on the first iteration.

To fix your problem, remove the else {} block.

Upvotes: 3

Related Questions