1

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);

    }
}
3
  • According to my understanding the Exception is thrown at: if (check.charAt(i) == vowel[i]) Commented Oct 11, 2014 at 11:03
  • You have mixed length of the string with the vowel array. Commented Oct 11, 2014 at 11:04
  • counter and vowel has 5 elements, check has 30 something. In other words, your loop is not constructed properly. Commented Oct 11, 2014 at 11:04

5 Answers 5

3

Your code reads like this: For each character in "check" if character at index in "check" is character at index in "vowel"

That's probably not what you're looking for. The exception you're getting is because there are only 5 characters in "vowel" but alot in "check" (i'm not counting)

Now, I'm assuming what you're wanting to do is actually count the number of each vowel in "check"

You should actually use a nested for loop in this case.

for (int i = 0; i < check.length(); i++) {
    for (int v = 0; v < vowel.length; v++) {
        if (check.charAt(i) == vowel[v]) {
            counter[v]++;
            break;
        }
    }
}
Sign up to request clarification or add additional context in comments.

Comments

2
for (int i = 0; i < check.length(); i++)
     if (check.charAt(i) == vowel[i])
         counter[i]++;

This loop goes from 0 to check.length(); but your array vowel[] has 5 element. So it generate array out of bound exception.

Comments

0

One more for the clever regex department so you don't have to make your string lowercase and lose the cases for vowels:

if (check.matches("(?i)[^a-z][a-z].*"))
    System.out.println("Vowels found: " + check.replaceAll("[^a-z]+", ""));

Comments

0

You must have two loops One for going through each char in string and a for loop going through each vowel array like so

 for (int i = 0; i < check.length(); i++)
     for (int p = 0; p < vowel.length; p++)
         if (check.charAt(i) == vowel[p])
             counter[p]++;

Enjoy.

1 Comment

Lol i know sorry jst changed now,difficult on tablet
0

You could use a regex for this operation:

Pattern vowels = Pattern.compile("(?i)[aeiou]");
String check = "Dit moet toch gewoon te doen zijn !!";

Matcher matcher = vowels.matcher(check);

while(matcher.find()) {
    System.out.println("found vowel: " + matcher.group());
}

Explanation of regex:

(?i) make pattern case insensitive, [aeiou] characters to match.

1 Comment

Just google regex if you want to know more. Very useful when working with strings.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.