I am new to Java. The purpose of the function is to get a word's vowel count. But when given ""
as input it returns 1
instead of 0
.
Please explain like i am five why this is happening.
public static int getCount(String str) {
String vowels = "aeiou";
int answer = 0;
String strArray[] = str.split("");
for(String chr : strArray) {
if ((vowels.contains(chr)) & (chr != "")) {
answer += 1;
}
}
return answer;
}
I fixed the code by adding if ((vowels.contains(chr)) & (chr != ""))
But this feels ugly