Count vowel in the word for method
public class Methods6 {
public static int countVowel(String words) {
int count = 0;
char[] vowel = {'a', 'e', 'i', 'o', 'u'};
for (int i = 0; i < words.length(); i++) {
char ch = words.charAt(i);
for (char cc : vowel) {
if (ch == cc) {
count++;
}
}
}
return count;
}
**Find max vowel in sentence **
public static String maxVowelWords() {
String sentence = getSentence().toLowerCase();
String words[] = sentence.split(" ");
int maxvowel = CountVowel(words[0]), count;
String maxWord = words[0];
for (int i = 1; i < words.length; i++) {
count = CountVowel(words[i]);
if (count > maxvowel) {
maxvowel = count;
maxWord = words[i] + " ";
}
}
return maxWord;
}}// 2 methods are located in the same Method Class
public class Test {
public static void main(String[] args) {
System.out.println(Methods6.MaxVowelWords());}}
When writing this method, it gives only the first word with the most vowel letters (eg. --------- hello my friend! ----------- just hello, hello and friend -------no! How can I change the method of this? Thanks for helps!
The way I interpret this question is that:
- Find the word in the supplied String which contains the most vowels in it.
- If one or more words within that very same String contain equal maximum number of vowels then concatenate the words together delimited by a whitespace (or whatever).
So, if the String supplied was: "hello my friend"
then both words, hello
and friend
, would be returned from the maxVowelWords() method in the form of (let's say):
hello, friend
Since both hello
and friend
contain 2 vowels which so happens to be the the max number of vowels in any given word then both words are returned.
If the supplied string however was: "hello my friend - I live on the Mississippi river."
then only Mississippi is returned from the method since it is the only word within that String that contains a maximun number of 4 vowels. All other words containing vowels within the string contain a lesser vowel count.
If this is indeed the situation then your method should look something like this:
public static String maxVowelWords() {
String sentence = getSentence();
String words[] = sentence.split(" ");
int maxvowel = 0;
String maxWord = "";
for (int i = 0; i < words.length; i++) {
int count = countVowels(words[i]);
if (count == maxvowel) {
maxvowel = count;
// Ternary Operator is used here to applt the delimiter (", ") if needed,
maxWord += (maxWord.equals("") ? words[i] : ", " + words[i]) + " (" + count + " vowels)";
}
else if(count > maxvowel) {
maxvowel = count;
maxWord = words[i] + " (" + count + " vowels)";;
}
}
return maxWord;
}
Yes, you start your loop from 0 and maxWord String variable should be initialized to hold an empty string (""). Let the for loop do its work. And No, count is not a waste, it's actually a requirement in order to to acquire the maximum vowel count for maxvowel as each string word is placed through the process.
You utilize a method named getSentence() to acquire the String to process and right away you distort that Original String by setting it to all lower case. You really shouldn't do that since the words you return from your maxVowelWords() method will not be the originally supplied words should they happen to have upper case vowels. A small modification to the countVowel() method can take care of that business, for example:
if (Character.toLowerCase(ch) == cc) {
count++;
}