Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Words with vowels in alphabetical order

Tags:

java

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;
}
like image 844
Timmy Avatar asked Nov 08 '13 14:11

Timmy


1 Answers

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);
}
like image 110
Patrick Avatar answered Sep 17 '22 23:09

Patrick