主题:[讨论]问题求助
读入一个TXT文本(仅有英文单词序列组成),统计各个单词出现的次数,并以适当的图形形式表示结果
我已经把文本读入了,也统计了各个单词出现的次数,可是不知道该怎样把结果用适当的图形形式显示出来。求高手帮忙,谢谢
import java.util.*;
import java.io.*;
public class zhonglv{
public static void main(String[] args) throws Exception {
Map hashMap = null;
BufferedReader infile = null;
StringTokenizer st = null;
String string;
String file = null;
//打开一篇文章,名字是 123.txt .
infile = new BufferedReader(new FileReader("f:\\123.txt"));
while ((string = infile.readLine()) != null) {
file += string; //读出整篇文章,存入String中。
}
hashMap = new HashMap();
// 取出文章中的单词,"," "." "!" " " "?"为各个单词的分界符。
st = new StringTokenizer(file, " ,.!?");
while (st.hasMoreTokens()) {
String key = st.nextToken();
if (hashMap.get(key) != null) {
int value = ((Integer) hashMap.get(key)).intValue();
value++;
hashMap.put(key, new Integer(value));
} else {
hashMap.put(key, new Integer(1));
}
}
//按照单词的字母次序输出。
Map treeMap = new TreeMap(hashMap);
Set entrySet = treeMap.entrySet();
Iterator iterator = entrySet.iterator();
while (iterator.hasNext())
{
System.out.println(iterator.next());
}
}
}
我已经把文本读入了,也统计了各个单词出现的次数,可是不知道该怎样把结果用适当的图形形式显示出来。求高手帮忙,谢谢
import java.util.*;
import java.io.*;
public class zhonglv{
public static void main(String[] args) throws Exception {
Map hashMap = null;
BufferedReader infile = null;
StringTokenizer st = null;
String string;
String file = null;
//打开一篇文章,名字是 123.txt .
infile = new BufferedReader(new FileReader("f:\\123.txt"));
while ((string = infile.readLine()) != null) {
file += string; //读出整篇文章,存入String中。
}
hashMap = new HashMap();
// 取出文章中的单词,"," "." "!" " " "?"为各个单词的分界符。
st = new StringTokenizer(file, " ,.!?");
while (st.hasMoreTokens()) {
String key = st.nextToken();
if (hashMap.get(key) != null) {
int value = ((Integer) hashMap.get(key)).intValue();
value++;
hashMap.put(key, new Integer(value));
} else {
hashMap.put(key, new Integer(1));
}
}
//按照单词的字母次序输出。
Map treeMap = new TreeMap(hashMap);
Set entrySet = treeMap.entrySet();
Iterator iterator = entrySet.iterator();
while (iterator.hasNext())
{
System.out.println(iterator.next());
}
}
}