I'm trying to optimize my Java code so I try things. In my search for a short way I produced the code here below. This throws an Exception. Can you tell me why? Can't I loop through a string by a char Array?
public class Input {
public static void main(String[] args) {
String check = "Dit moet toch gewoon te doen zijn !!";
check = check.toLowerCase();
int[] counter = {0, 0, 0, 0, 0};
char[] vowel = {'a', 'e', 'i', 'o', 'u'};
int total = 0;
for (int i = 0; i < check.length(); i++)
if (check.charAt(i) == vowel[i])
counter[i]++;
for (int t : counter)
total += t;
System.out.println("Aantal klinkers: \t" + total);
}
}
if (check.charAt(i) == vowel[i])counterandvowelhas 5 elements,checkhas 30 something. In other words, your loop is not constructed properly.