基本上我正在尝试使用字符串作为键(单个字符)和整数变量(字符频率)来实现映射。
程序运行时我得到不准确的频率。有谁知道我哪里出错了?
import java.io.File;
import java.io.FileNotFoundException;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
public class WordCount {
public static void main(String[] args) throws FileNotFoundException{
File f1 = new File("article.txt");
Scanner scan = new Scanner(f1).useDelimiter("|");
Map<String, Integer> wordMap = new HashMap<String, Integer>();
String letter;
Integer value = 0;
while(scan.hasNext()){
letter = scan.next();
if(wordMap.containsKey(letter)) wordMap.put(letter, value++);
else wordMap.put(letter, new Integer(value++));
}
System.out.println(wordMap);
}
}