2016-09-08 17 views
0

私は学校でやっているコードに問題があります。私の論理の中でそれを維持しようとしている(そして本質的に失敗している)。この仕事をする上でヒントがあるかどうか疑問に思うだけです。BufferedReaderを使用してカウンタを含む2次元配列にする

public static String[][] sortWords(BufferedReader in, int n) throws IOException{ 
    String line = ""; 
    int ctr = 0; 
    String[][] words = new String[n][2]; 

    for(int m = 0; m < n; m++) { 
     words[m][1] = "1"; 
    } 

    while((line=in.readLine())!=null) { 
     String a[]=line.split(" ");  
     for(int i = 0; i < a.length; i++) { 
      a[i] = a[i].toUpperCase(); 
      for(int h = ctr; h < n; h++) { 
       if (words[h][0].equals(a[i])) { 
        words[h][1] = "" + (Integer.parseInt(words[h][1])+1); 
       } else{ 
        words[ctr][0] = a[i]; 
        ctr++; 
        break; 
       } 
      } 
     } 
     line=in.readLine(); 
    } 
    return words; 
} 

私がしようとしているのは、かなり大きい(70kワード)のtxtファイルを取り出して解読することです。私が考えていたこの方法は、以下を行うことができました。 - ファイル内のすべての単語を検索 - 各単語の出現数を確認します。 - アクセスしやすいように、両方の値を2次元配列に格納します。

私が拠点を外れているとわかっています。 ありがとうございます。

+0

;' –

+0

あなたは1から開始する回数を設定したように、カウンターを更新して1回の出現とカウント== 2で終了する単語の最初の出現を見つけました。0から始めるか、ループ内の初期化部分を移動する必要があります。さらに、配列の長さを行数に設定しますが、見つけることのできる単語はそれ以上に可能です。 –

+0

単語とその出現を 'HashMap 'に格納する方が簡単です。新しい単語が見つかるたびに、その単語を 'HashMap'に置き、その値を' 1'にします。すでにハッシュに入っている場合は、 '++ 'などを使って値を更新します。 – Justplayit94

答えて

0

すべてのコメントがポイントですが、私はそれらをコードに翻訳しようとします。各ステップで、変更されていないすべての行をコメントアウトして、変更がより明確になるようにしました。

最初に、その2次元配列をねじます。制限的で扱いにくいです。のではなく、地図を使用してみましょう:

public static Map<String, Integer> sortWords(BufferedReader in) throws IOException{ 
// String line = ""; 
    Map<String, Integer> wordsCount = new HashMap<>(); 
// 
// while((line=in.readLine())!=null) { 
//  String a[]=line.split(" "); 
//  for(int i = 0; i < a.length; i++) { 
//   a[i] = a[i].toUpperCase(); 
      Integer count = wordsCount.get(a[i]); // Get current count for this word 
      if (count == null) count = 0; // Initialize on first appearance 
      count++; // Update counter 
      wordsCount.put(a[i], count); // Save the updated value 
//  } 
//  line=in.readLine(); 
// } 
// return words; 
//} 

ちょうどその単語に関連する値を取得し、それを更新...配列は、追加のループ、無Stringintへの変換を初期化する必要はありませんが。そして、今では単語の数を事前に知る必要はないので、2番目のint nパラメータは安全に削除できます!

ここでは、2000年以前の非常に基本的な、Cのような2000年のイディオム(すべてfor(;;)と配列など)を使用していることがわかりました。それは完全に有効ですが、あなたはより現代的でより有用な構成を見逃しています。ですので、私たちは2004年から利用可能ですか?enhanced for loop

//public static Map<String, Integer> sortWords(BufferedReader in) throws IOException{ 
// String line = ""; 
// Map<String, Integer> wordsCount = new HashMap<>(); 
// 
// while((line=in.readLine())!=null) { 
//  String a[]=line.split(" "); 
     for(String word : a) { 
      word = word.toUpperCase(); 
      Integer count = wordsCount.get(word); // Get current count for this word 
//   if (count == null) count = 0; // Initialize on first appearance 
//   count++; // Update counter 
      wordsCount.put(word, count); // Save the updated value 
//  } 
//  line=in.readLine(); 
// } 
// return wordsCount; 
//} 

鮮明構文は、我々はループの中で扱っているオブジェクトの種類を正確に知っている...と、すべての最高は、それはそれはきれいにするためにあなたにインラインあなたのコードの一部をすることができます。今toUpperCase()方法ではなく、単語ごとに一度の行ごとに一度だけ呼び出されます、そして我々は

最後;-P左皆の目を傷つけたことString a[]を処分した

//public static Map<String, Integer> sortWords(BufferedReader in) throws IOException{ 
// String line = ""; 
// Map<String, Integer> wordsCount = new HashMap<>(); 
// 
// while((line=in.readLine())!=null) { 
     for(String word : line.toUpperCase().split(" ")) { 
//   Integer count = wordsCount.get(word); // Get current count for this word 
//   if (count == null) count = 0; // Initialize on first appearance 
//   count++; // Update counter 
//   wordsCount.put(word, count); // Save the updated value 
//  } 
//  line=in.readLine(); 
// } 
// return wordsCount; 
//} 

を:このように最後にその余分なreadLine()を取り除くことです。それでは、あなたのコードは次のようになります:

public static Map<String, Integer> sortWords(BufferedReader in) throws IOException { 
    String line = ""; 
    Map<String, Integer> wordsCount = new HashMap<>(); 

    while ((line = in.readLine()) != null) { 
     for(String word : line.toUpperCase().split(" ")) { 
      Integer count = wordsCount.get(word); // Get current count for this word 
      if (count == null) count = 0; // Initialize on first appearance 
      count++; // Update counter 
      wordsCount.put(word, count); // Save the updated value 
     } 
    } 
    return wordsCount; 
} 

非常によかった!
次のようなメソッドを使用することができます。

BufferedReader in = new BufferedReader(new FileReader("myWords.txt")); 
Map words = sortWords(in); 
int numberOfHellos = words.get("Hello"); 
int numberOfGreetings = numberOfHellos + words.get("Hi") + words.get("Howdy"); 
あなたはこのコード `}}}ライン= in.readLine()で行をスキップします
+0

こんにちはWalen、返事ありがとう、それは非常に洞察力があったと私はそれから必要なすべてを得た。一般的なHashMapsに関する質問ですが、私はいくつかのGoogleやYouTubeの研究を始めました。キーと値があるようですが、単語がキーになるようにコードを作成しました(したがって、正確なカウントを作成するためにユニークです)。テキストファイル内のその単語のカウント値? – DCDCDC

+0

もう一度ありがとうございます。これは完全な問題の概要と私が結局何をしたのか - https://github.com/AussieDropBear/H274 答えを得られないことを理解したかったので、私はすべてを投稿したくなかったので、もう一度あなたの助けに感謝します: ) – DCDCDC

関連する問題