How do I check if a char is a vowel?

Kylo picture Kylo · Oct 3, 2013 · Viewed 65.2k times · Source

This Java code is giving me trouble:

    String word = <Uses an input>
    int y = 3;
    char z;
    do {
        z = word.charAt(y);
         if (z!='a' || z!='e' || z!='i' || z!='o' || z!='u')) {
            for (int i = 0; i==y; i++) {
                wordT  = wordT + word.charAt(i);
                } break;
         }
    } while(true);

I want to check if the third letter of word is a non-vowel, and if it is I want it to return the non-vowel and any characters preceding it. If it is a vowel, it checks the next letter in the string, if it's also a vowel then it checks the next one until it finds a non-vowel.

Example:

word = Jaemeas then wordT must = Jaem

Example 2:

word=Jaeoimus then wordT must =Jaeoim

The problem is with my if statement, I can't figure out how to make it check all the vowels in that one line.

Answer

Silviu Burcea picture Silviu Burcea · Oct 3, 2013

Clean method to check for vowels:

public static boolean isVowel(char c) {
  return "AEIOUaeiou".indexOf(c) != -1;
}