2012-11-02 38 views
2

我想知道是否有人對如何通過取出句子列表返回帶有最多元音的單詞有任何意見。返回java中最多元音字母

我知道如何計算單詞中的元音並返回計數。只是無法與大多數元音返回字..

任何建議,將不勝感激

+1

商店當前詞最元音和當前元音變量數。當你循環你的單詞,如果你檢查的單詞有更多的元音,那麼當前的冠軍,然後用該單詞替換當前的冠軍,並計數你的檢查。 –

+0

+1:沒想到對這樣一個簡單問題的答案如此多樣。 – Dmitri

+0

可以有多個獨特的單詞與最多的元音數量,所以記得要返回設置。 – Srimathi

回答

0

商店字一個字符串變量和循環結束後返回的字符串。

0

您可以將每個句子分成(這個詞是一個提示)到List的單詞中,並獲得最大元音量的單詞。
您將以每個句子的一個單詞(「最大」)結束,再次處理這個List的單詞(您可以在這裏做一個簡潔的遞歸調用),並且您將獲得文字中含有最多元音的單詞。

我建議你看看由Collections提供的靜態方法,尤其是:

public static <T> T max(Collection<? extends T> coll, Comparator<? super T> comp) 

,並最終

public static <T> void sort(List<T> list, Comparator<? super T> c) 


所有你所要做的就是實現一個Comparator這之間確定兩個詞是哪一個元音的數量最多。這幾乎是兩行代碼,聽起來像是作業。

編輯:最後,因爲他到處都是劇透,那就是解決方案。隨着記憶化有一種方法,以商更好的性能,但是這不是重點我想

final static Comparator <String> vowelComparator = new Comparator<String>() { 
    @Override 
    public final int compare(final String word1, final String word2) { 
     return vowelCount(word1) - vowelCount(word2); 
    } 

    private final int vowelCount(final String word) { 
     int count = 0; 
     for (final char c : word.toCharArray()) { 
      if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u') 
       count++; 
     } 
     return count; 
    } 
}; 

public static void main(String...args) { 
    //Sentences 
    final List<String> sentences = new ArrayList<String>(3){{ 
     add("This is my first line"); 
     add("Hohoho is my second astonishing line"); 
     add("And finally here is my last line"); 
    }}; 

    //Store the words with highest number of vowels/sentence 
    final List<String> interestingWords = new LinkedList<>(); 
    for (final String sentence : sentences) { 
     interestingWords.add(Collections.max(Arrays.asList(sentence.split(" ")), vowelComparator));  
    } 

    System.out.println(Collections.max(interestingWords, vowelComparator)); 
} 
0
String[] words = yourString.split(" "); 
int max = 0; 
for(String myStr: words) 
    max = Math.max(max,myStr.split("[aeiou]").length); 
for(String myStr: words) 
    if(myStr.split("[aeiou]").length == max) 
     return myStr; 
+0

似乎是一個不必要的昂貴的數字計數方式......尤其是如果你這樣做了兩次。 – Dmitri

+0

我想你可以跟蹤哪個字符串具有最大值,但它確實沒有必要,因爲編譯器應該優化它。 – AJMansfield

+0

@AJMansfield不是很有建設性的答案。也許可以嘗試一個不太簡潔的代碼示例,它可能更具信息性和指導性? – travega

3
String myString = "Java is magical"; 

// 1. Split your string into an array of words. 
String[] words = myString.split(" "); 

// 2. Initialise a max vowel count and current max count string variable 
int maxVowelCount = 0; 
String wordWithMostVowels = ""; 

// 3. Iterate over your words array. 
for (String word : words) { 
    // 4. Count the number of vowel in the current word 
    int currentVowelCount = word.split("[aeiou]", -1).length; 

    // 5. Check if it has the most vowels 
    if (currentVowelCount > maxVowelCount) { 

     // 6. Update your max count and current most vowel variables 
     wordWithMostVowels = word; 
     maxVowelCount = currentVowelCount; 
    } 
} 

// 6. Return the word with most vowels 
return wordWithMostVowels; 

你可能會想在一個方法來包裝這個功能,併到過你「的myString的價值方法。

+0

它不適用於重複的元音,嘗試與「scenarii」。 – Jerome

+0

需要一個'-1'參數來分割。也不妥善處理案件。嚴格來說,'currentWordCount'和'maxVowelCount'不是元音計數,可以比較,但調用變量不是非常糟糕的風格。 – Dmitri

+0

@Dmitri感謝您的質量控制,這就是爲什麼這是網上最好的編程資源...如果你願意的話,你現在可以恢復我的+1;) – travega

2

這裏的東西我會考慮簡單,易讀,正確的是:

public static String findMaxVowels(Collection<String> text) { 
    String best = null; 
    int max = 0; 
    for (String line : text) { 
     // may need a better definition of "word" 
     for (String word : line.split("\\s+")) { 
      int count = countChars(word.toLowerCase(), "aeiou"); 
      if (count > max) { 
       max = count; 
       best = word; 
      } 
     } 
    } 
    return best; 
} 

public static int countChars(String text, String chars) { 
    int count = 0; 
    for (char c : text.toCharArray()) 
     if (chars.indexOf(c) >= 0) 
      count += 1; 
    return count; 
} 
相關問題