lundi 2 octobre 2017

Loop doesn't iterate an if statement more than once

So, the original problem that I am attempting to solve with this code is taking a string of varying length and then returning true only if that string contains between 1-3 "e's" and if there are any less or more return false. I wanted to individually extract the "e's" from the the given Strings and put them into a separate String and then test for how many "e's" that new String has to produce the correct Boolean values. Isolating the pol execution, by putting 3 "e's" into the empty string, I found that the code was returning false for everything that had at least 1 e. Nothing wrong there, but then I replaced the empty string with 2 "e's" and found that anything with at least 1 e was returning true, even if that String contained 50 "e's" in total. This means that the loop, when encountering the if statement, only iterates once and so only 1 e is added to String pol. My overarching question is : How do I get the loop to iterate the if statement according to the control.

Also don't worry about what comes before this code: Only know that this is Boolean

String pol = "";
String some;
for (int len = 0; len < str.length(); len = len + 1) {
    some = str.substring(len, len + 1);
    if (some.equals("e")) {
        pol = "" + some; 
    }
}
if (pol.equals("e") || pol.equals("ee") || pol.equals("eee")) 
    return true;
return false; 

Aucun commentaire:

Enregistrer un commentaire