Words with vowels in alphabetical order

You can simply use a regular expression in your method :

public static boolean containsVowels(String word) {
    return Pattern.matches(".*a.*e.*i.*o.*u.*y.*", word);
}

Use a regular expression

if (word.matches("[^aeiou]*a[^aeiou]*e[^aeiou]*i[^aeiou]*o[^aeiou]*u[^aeiou]")){
    //found one
}

Where [^aeiou]* means zero or more consonants; ^ in a regular expression means none of the stuff in [...].

It might not be the quickest solution but it is clear; especially if you form the regular expression without hardcoding [^aeiou] many times as I have.

Edit: @Patrick's regular expression is superior.


The containsVowels will only return true if the string "aeiouy" is a substring of the, word, like this:

"preaeiouy","aeiouypost","preaeiouypost"

This would be a more correct method:

public static boolean containsVowels(String word) {
    String vowels = "aeiouy";
    if (word == null || word.length() < vowels.length())
        return false;
    int counter = 0;
    int vowelCounter = 0;
    //Loop until the whole word has been read, or all vowels found
    while(counter<word.length() && vowelCounter < vowels.length()){
        if (word.charAt(counter) == vowels.charAt(vowelCounter)){
            vowelCounter++;
        }
        counter++;
    }
    return vowelCounter == vowels.length();
}

Tags:

Java