2017-01-16 4 views
-8

私は文章を持っており、最も多くの単語に現れる文字とそれに含まれる単語を見つけたいと思います。 たとえば、「フロリダ州オーランドに住んでいる私の友人ウィルを訪ねるのが好きです。 出力先はI 8です。Javaプログラムは、ほとんどの単語に表示される文字を見つけるには?

 char maxChar2 = '\0'; 
     int maxCount2 = 1; 
     for (int j=0; j<strs2.length; j++) { 
     int charCount = 1; 
     char localChar = '\0'; 
     for (int k=0; k<strs2[j].length(); k++) { 
      if (strs2[j].charAt(k) != ' ' && strs2[j].charAt(k) != maxChar2) { 
       for (int l=k+1; l<strs2[j].length(); l++) {  
        if (strs2[j].charAt(k)==strs2[j].charAt(l)) { 
         localChar = strs2[j].charAt(k); 
         charCount++; 
        } 
       } 
      } 
     } 
     if (charCount > maxCount2) { 
      maxCount2 = charCount; 
      maxChar2 = localChar; 
     } 
    } 

、strs2は、文字列の配列です: これは私のコードです。 私のプログラムは私にO 79を与えています。また、大文字と小文字は関係ありませんし、すべての句読点を避けてください。絆がある場合、これが唯一の出力文字最初に達したことは以下となります。ここでは

+2

[宿題に関する質問や回答はどうすればいいですか?](// meta.stackoverflow.com/q/334822) – Tom

+0

本、ツール、ソフトウェアライブラリ、チュートリアルなどのオフサイトリソーススタックオーバーフローの話題にはなりません。なぜなら、オピニオン回答とスパムを引き付ける傾向があるからです。代わりに、問題を説明し、それを解決するためにこれまでに何が行われているかを記述します。 – nullpointer

+0

出力は 'I 8'ではなく' I 7'でなければなりません。 ** I **、** ** ** **、** ** i **、**、** ** ** ** **、 ** i ** ves、** i ** n、Flor ** i ** da。このコードで証明されているように:[IDEONE](http://ideone.com/Tqpgz1) – Andreas

答えて

-1

String input = "I like visiting my friend Will, who lives in Orlando, Florida."; 
FindMostPopularCharacter(input); 

public static void FindMostPopularCharacter(String input) 
{ 
    String alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; 
    input = input.toUpperCase(); 
    HashMap<Character, Integer> charData = new HashMap<>(); 
    char occursTheMost = 'A'; //start with default most popular char 
    int maxCount = 0; 

    //create the map to store counts of all the chars seen 
    for(int i = 0; i < alphabet.length(); i++) 
     charData.put(alphabet.charAt(i), 0); 


    //first find the character to look for 
    for(int i = 0; i < input.length(); i++) 
    { 
     char c = input.charAt(i); 
     //if contained in our map increment its count 
     if(charData.containsKey(c)) 
      charData.put(c, charData.get(c) + 1); 
     //check for a max count and set the values accordingly 
     if(charData.containsKey(c) && charData.get(c) > maxCount) 
     { 
      occursTheMost = c; 
      maxCount = charData.get(c); 
     } 
    } 
    //final step 
    //now split it up into words and search which contain our most popular character 
    String[] words = input.split(" "); 
    int wordCount = 0; 
    CharSequence charSequence; 
    for(Character character : charData.keySet()) 
    { 
     int tempCount = 0; 
     charSequence = "" + character; 
     for(int i = 0; i < words.length; i++) 
     { 
      if(words[i].contains(charSequence)) 
       tempCount++; 
     } 

     if(tempCount > wordCount) 
     { 
      occursTheMost = character; 
      wordCount = tempCount; 
     } 
    } 

    System.out.println(occursTheMost + " " + wordCount); 
} 

出力が

I 8 

ノートでややエレガントなソリューションです発生回数の最大値。

FindMostPopularCharacter("aabb aabb aabb bbaa"); 

出力Bは、第1の入力における最後の単語による前maxに達するため

B 4 

FindMostPopularCharacter("aab aab b") 

B 3 
+1

結果が正しくありません。 'FindMostPopularCharacter(" aab aab b ")'は 'A 2'を出力しますが、答えは' B 3'です。あなたの全体の前提は、最も出現している手紙もほとんどの言葉で発生しているということです。これは誤った仮定であることが証明されているため、コードは正しい結果を生み出すことができません。 – Andreas

+0

@Andreas良いキャッチ、私は誤解し、彼はどのキャラクターが文章の中で最も多く見つかったか、そしてどれくらいの単語が見つかったかを見つけたいと思っていました。 –

0

ヒントとして、より意味のある変数名と適切な字下げを使用してみてください。これは特にあなたのプログラムがあなたがしなければならないと思ったことをしていないときに大いに役立ちます。また、より小さなものを始め、いくつかのテストを書くことは束を助けるでしょう。完全な文章の代わりに、2つの単語、次に3つの単語、そしてより精巧な文章で作業してください。

少し読みやすくするようにコードを書き換える:

// Where sentence is: "I like".split(" "); 
private static void getMostFrequentLetter(String[] sentence) { 
    char mostFrequentLetter = '\0'; 
    int mostFrequentLetterCount = 1; 

    for (String word : sentence) { 
     int charCount = 1; 
     char localChar = '\0'; 

     for (int wordIndex = 0; wordIndex < word.length(); wordIndex++) { 
      char currentLetter = word.charAt(wordIndex); 

      if (currentLetter != ' ' && currentLetter != mostFrequentLetter) { 
       for (int l = wordIndex + 1; l < word.length(); l++) { 
        char nextLetter = word.charAt(l); 

        if (currentLetter == nextLetter) { 
         localChar = currentLetter; 
         charCount++; 
        } 
       } 
      } 
     } 

     if (charCount > mostFrequentLetterCount) { 
      mostFrequentLetterCount = charCount; 
      mostFrequentLetter = localChar; 
     } 
    } 
} 

今私がしたすべてはあなたの変数の名前を変更してのfor-eachループにループのためにあなたを変更しました。これを行うと、アルゴリズムと実行しようとしていることをより明確に見ることができます。基本的には、各単語を調べて現在の文字と次の文字を比較して重複をチェックします。私が "私が好き"とこれを実行する場合、私はi 2を取得する必要がありますが、代わりにnull char 1を取得します。あなたは、普通の手紙を適切に比較して保存していません。これはあなたに答えを与えるものではありませんが、これによりコードが何をしているのかをより明確にして、修正することができれば幸いです。

関連する問題