[英]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循環內的哈希映射中。 因為它是循環所有行,然后處理最后一行。
哇,你讓這個變得復雜了。
研究Java String split方法。
考慮一下您的哈希圖。 為了進行計數,每個唯一單詞只需要一個條目。 因此,在偽代碼中,您需要類似:
為文件中的每一行打開文件對每一行中的每個單詞執行操作如果沒有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.