Alphabetic vowels

The goal of the program is to return words from a list of words that have all 6 vowels (including y). Where are the vowels in alphabetical order. For example, the answer might be something like: Aerious (Aerious doesn't work, although it doesn't have y). Currently, the program does not return any words. I do not 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; } 
+6
source share
4 answers

Only contains true if the string "aeiouy" is a substring of a word, for example:

"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(); } 
+2
source

You can simply use regex in your method:

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

Use regex

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

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

This may not be the fastest solution, but it is understandable; especially if you form a regular expression without hardcoding [^aeiou] many times like me.

Edit: @Patrick's regex is superior.

+3
source

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 } 
+2
source

Source: https://habr.com/ru/post/957680/


All Articles