2017-10-13 29 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+")因为它的自由文本格式。

相关问题