2017-10-13 14 views
1

タイトルには、単純なテキストファイルを読み込み、個々の単語をハッシュマップに送信しようとしています。私は、しかし、次のCJava - .txtファイルからの単語をHashMapに入れますか?

import java.util.*; 
import java.io.*; 

public class Profile{ 

    public static String file; 
    public static int len; 
    public static int count = 0; 
    public static String[] words; 
    public static String[] unrepeatedWords; 

    public static Map<String, Integer> record = new HashMap<String, Integer>(); 
    //Integer count = record.get(word); 
    //Integer count = record.get(word); 
    Set<String> keySet = record.keySet(); 



//Method to read whole file 
    static void wholeFile(File file){ 
    try { 
      Scanner in = new Scanner(file); 
      int lineNumber = 1; 

      while(in.hasNextLine()){ 



       String line = in.nextLine(); 
       //count += new StringTokenizer(line, " ,").countTokens(); 
       //System.out.println(line); 
       words = line.split("/t"); 
       words = line.split(" "); 
       //System.out.println(words + ""); 
       lineNumber++; 
      } 
      for(String word : words){ 
      //System.out.println(word); 
      if(!record.containsKey(word)){ record.put(word, 1); } 
      if(record.containsKey(word)){ record.put(word, record.get(word) + 1); } 
      } 
      System.out.println(record); 
      in.close(); 

     } catch (Exception ex) { 
      ex.printStackTrace(); 
     } 

    } 

    Profile(String file){ 
    this.file = file; 
    } 
    Profile(String file, int len){ 
    this.file = file; 
    this.len = len; 
    } 
    public static void main(String[] args){ 
     file = args[0] + ""; 
     File a = new File(file); 
     //Scanner in = new Scanner(a); 

     wholeFile(a); 
    } 
} 

を書かれている

it was the best of times 
it was the worst of times 

it was the age of wisdom 
it was the age of foolishness 

it was the epoch of belief 
it was the epoch of incredulity 

it was the season of light 
it was the season of darkness 

it was the spring of hope 
it was the winter of despair 
see the test 
try this one 

:私は最終的に私は、次のテキストファイル(TEXT.TXT)を持っているハッシュマップ、周波数に各単語をカウントするように私のプログラムを構築します私は、コマンドの実行プロファイルTEXT.TXTを実行したとき、私は唯一のHashMapに最後の行を格納しています:

> run Profile text.txt 
{one=2, this=2, try=2} 
> 

私が間違って何をしているのですか? HashMapの内部にある.txtファイル内のすべての単語を効率的に保存するにはどうすればよいですか?アドバイスが参考になります。

+0

デバッガを使用するか、いくつかの 'System.out.println'行を追加して、何が起きているかを確認することをお勧めします。小さなバグは2つしかありません。 –

答えて

1

他の回答に記載されているとおり、splitを処理するforの配置が間違っています。あなたはまた、任意の意味をなさない二つの連続分割をしていた

while (in.hasNextLine()) { 
    String line = in.nextLine(); 
    words = line.split(" "); 

    //here so it can use the split from the previous line 
    for (String word : words) { 
     if (!record.containsKey(word)) { 
      record.put(word, 1); 
     } 
     else { 
      record.put(word, record.get(word) + 1); 
     } 
    } 
} 

注:それはそうのように、while内にある必要があります。そのフリーテキスト形式のでsplit("\\s+")を使用する

代わりに、より良いsplit(" ")

-1

データを.jsonファイルとして保存し、標準のjson形式にフォーマットすることを検討する必要があります。あなたのデータを解析してください。

0

whileループ内の単語をハッシュマップに入れているforループを置く必要があります。それはすべての行をループして、最後の行を処理します。

0

うわー、あなたはこれを複雑にしています。

  1. Java String splitメソッドを調べます。

  2. ハッシュマップについて考えてみましょう。カウントするには、一意の単語ごとに1つのエントリだけが必要です。

    ファイル 内の各ラインのために開いたファイル 行 の各単語のため を行う場合ではないmap.containsKey(ワード) map.put(ワード、1) を を行いますので、擬似コードでは、あなたは次のように何かをしたいです他 - 突然、SOコードとしてそれをフォーマットしないであろう結果

で何かをここ Fiの OD OD あなたのカウントをインクリメント。 string.Splitを使用するを更新しました

Here's a screenshot:

。くそったwhippersnappers。

+1

'StringTokenizer'を調べるのは悪い考えです。 [docs](http://download.java.net/java/jdk9/docs/api/java/util/StringTokenizer.html)から: "' StringTokenizer'は互換性の理由から継承されているレガシークラスですこの機能を求めている人は、代わりにStringのsplitメソッドまたはjava.util.regexパッケージを使用することをお勧めします。 " – bcsb1001

+0

StringTokenizerはAbraham Lincolnのために十分で、それでも十分です。 –

0

while (in.hasNextLine())ループ内for(String word : words)ループを置きます。

関連する問題