繁体   English   中英

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

[英]Java - Putting words from .txt file into 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);  
  }
}

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

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

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

正如其他答案所指出的那样,您错位了您for ,无法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);
        }
    }
}

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

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

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

哇,你让这个变得复杂了。

  1. 研究Java String split方法。

  2. 考虑一下您的哈希图。 为了进行计数,每个唯一单词只需要一个条目。 因此,在伪代码中,您需要类似:

    为文件中的每一行打开文件对每一行中的每个单词执行操作如果没有map.containsKey(word)map.put(word,1)否则-在此处增加计数,以便对结果进行某些处理

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

这是屏幕截图:

更新为使用String.split。 该死的wh子。

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM