Timmy
Timmy

Reputation: 67

Words with vowels in alphabetical order

The goal of the program is to return the words from a wordlist that have all 6 vowels (including y). Where the vowels are in alphabetical order. For example, an answer might be something like: Aerious (Aerious would not work though, since it does not have a y). Currently the program doesn't return any words. I don't think the containsVowels method is correct.

public static void question11() {
    System.out.println("Question 11:");
    System.out.println("All words that have 6 vowels once in alphabetical order: ");
    String vowelWord = "";

    for (int i = 1; i< WordList.numWords(); i++) {
        if (containsVowels(WordList.word(i))) {       
            if (alphabetical(WordList.word(i))) {
                vowelWord = WordList.word(i);
                System.out.println(vowelWord);
            }
        }
    }

    return;
}

public static boolean alphabetical(String word) {
    int vowelPlaceA = 0;
    int vowelPlaceE = 0;
    int vowelPlaceI = 0;
    int vowelPlaceO = 0;
    int vowelPlaceU = 0;
    int vowelPlaceY = 0;

    for (int i = 0; i < word.length(); i++) {
        if (word.charAt(i) == 'a') {
            vowelPlaceA = i;
        }
        if (word.charAt(i) == 'e') {
             vowelPlaceE = i;
        }
        if (word.charAt(i) == 'i') {
             vowelPlaceI = i;
        }
        if (word.charAt(i) == 'o') {
             vowelPlaceO = i;
        }
        if (word.charAt(i) == 'u') {
             vowelPlaceU = i;
        }
        if (word.charAt(i) == 'y') {
             vowelPlaceY = i;
        }
        //check a alphabetical
        if(vowelPlaceA > vowelPlaceE || vowelPlaceA > vowelPlaceI || vowelPlaceA > vowelPlaceO ||
          vowelPlaceA > vowelPlaceU || vowelPlaceA > vowelPlaceY) {
             return false;
        }
        //check e alphabetical
        if(vowelPlaceE > vowelPlaceI || vowelPlaceE > vowelPlaceO ||
          vowelPlaceE > vowelPlaceU || vowelPlaceE > vowelPlaceY) {
             return false;
        }
        //i
        if(vowelPlaceI > vowelPlaceO || vowelPlaceI > vowelPlaceU || vowelPlaceI > vowelPlaceY) {
             return false;
        }
        //o
        if(vowelPlaceO > vowelPlaceU || vowelPlaceO > vowelPlaceY) {
             return false;
        }
        //u
        if(vowelPlaceU > vowelPlaceY) {
             return false;
        }    
    }
    return true;
}

public static boolean containsVowels(String word) {
    String vowels = "aeiouy";
    if (word.contains(vowels)) {
        return true;
    }
    return false;
}

Upvotes: 6

Views: 8649

Answers (4)

St&#233;phane Bruckert
St&#233;phane Bruckert

Reputation: 22933

The logic without regex.

public static boolean containsVowels(String word) throws NullPointerException {
    List<String> vowels = new ArrayList<String>(
                               Arrays.asList("a", "e", "i", "o", "u", "y"));
    String lastVowel = vowels.get(vowels.size() - 1);
    for (String c : vowels) {
        //false is returned if one vowel can't be found
        if (!word.contains(c)) {
            return false;
        }
        //true is returned once the last vowel is found (as the previous ones)
        if (c.equals(lastVowel)) {
            return true;
        }
    }
    //will never go out of the loop
}

Upvotes: 2

Bathsheba
Bathsheba

Reputation: 234785

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.

Upvotes: 3

Plux
Plux

Reputation: 412

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

Upvotes: 2

Patrick
Patrick

Reputation: 831

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

Upvotes: 7

Related Questions