Java - 将.txt文件中的单词放入HashMap?

时间:2017-10-13 22:33:54

标签: java hashmap

正如标题所述,我试图阅读一个简单的文本文件并将单个单词提交到哈希映射中。我最终将构建我的程序来计算每个单词的频率,HashMaps我有以下文本文件(text.txt):

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

但是,当我运行命令运行Profile text.txt时,我只将最后一行存储到HashMap中:

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

我做错了什么?如何有效地将.txt文件中的所有单词存储在HashMap内?任何建议都会有所帮助。

5 个答案:

答案 0 :(得分:1)

正如其他答案所述,您错过了处理for的{​​{1}}。它应该在split内,如下所示:

while

请注意,您还连续进行了两次拆分,但没有任何意义。

答案 1 :(得分:0)

你需要把把这些单词放入while循环中的哈希映射的for循环。因为它是循环所有行,然后处理最后一行。

答案 2 :(得分:0)

哇,你把这复杂化了。

  1. 调查Java String split方法。

  2. 想想你的哈希地图。对于计数,您只需要为每个唯一单词输入一个条目。所以在伪代码中,你需要类似的东西:

    打开文件 对于文件中的每一行 做    对于每个单词    做        如果没有map.containsKey(word)           map.put(word,1)        其他            - 在这里增加你的计数        科幻    OD OD 做一些结果

  3. 突然间,SO不会将其格式化为代码。

    Here's a screenshot:

    已更新以使用String.split。该死的鞭挞者。

答案 3 :(得分:0)

for(String word : words)循环放在while (in.hasNextLine())循环

而不是split(" ")更好地使用split("\\s+")因为它的自由文本格式。

答案 4 :(得分:-1)

您应该考虑将数据存储为.json文件,将其格式化为标准的json格式。然后解析你的数据