2017-10-13 30 views
1

正如標題所述,我試圖讀取一個簡單的文本文件並將單個單詞提交到哈希映射中。我最終將構建我的程序計數頻率每個字,其中包含HashMap我有以下的文本文件(的text.txt):Java - 將.txt文件中的單詞放入HashMap中?

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 

我已經寫了下面的C

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); 
    } 
} 

然而,當我運行命令運行配置文件text.txt,我只存儲到HashMap的最後一行:

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

我做錯了什麼?如何有效地存儲HashMap中的.txt文件中的所有單詞?任何建議都會有幫助。

+0

我建議使用調試器或添加一些'System.out.println'行來查看是否可以關注發生的事情 - 只有2個小錯誤。 –

回答

1

正如其他答案所述,您錯過了您的for處理split。它應該是while裏面,像這樣:

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); 
     } 
    } 
} 

注意,你也做兩個連續的分裂不作任何意義。

-1

您應該考慮將數據存儲爲.json文件,並將其格式化爲標準json格式。然後解析您的數據

0

您需要放置將單詞放入while循環內的哈希映射中的for循環。就像你循環所有行,然後處理最後一行。

0

哇,你讓這很複雜。

  1. 調查Java String split方法。

  2. 想想你的哈希映射。對於計數,您只需要爲每個唯一的單詞輸入一個條目。因此,在僞代碼,你想要的東西,如:

    打開文件 在文件 每一行的每個字做 符合 做 如果不是map.containsKey(字) map.put(字,1) 否則 - 增加你這裏算 網絡 OD OD 做一些事情的結果

突然這麼不會格式化的代碼。

Here's a screenshot:

更新使用String.split。該死的whippersnappers。

+1

調查'StringTokenizer'可能是一個壞主意。來自[文檔](http://download.java.net/java/jdk9/docs/api/java/util/StringTokenizer.html):「'StringTokenizer'是一個遺留類,儘管使用它,但由於兼容性原因而被保留在新代碼中不鼓勵,建議任何尋求這種功能的人都使用String或java.util.regex包的拆分方法。「 – bcsb1001

+0

StringTokenizer對亞伯拉罕林肯來說足夠好,對我來說這已經足夠了。 –

0

for(String word : words)循環內while (in.hasNextLine())

代替split(" ")更好地使用split("\\s+")因爲它的自由文本格式。

相關問題