import java.io.*;
import java.util.*;
class A {
public static void main(String args[]) throws Exception {
Console con = System.console();
String str;
int i=0;
HashMap map = new HashMap();
HashSet set = new HashSet();
System.out.println("Enter File Name : ");
str = con.readLine();
File f = new File(str);
f.createNewFile();
FileInputStream fis = new FileInputStream(str);
StreamTokenizer st = new StreamTokenizer(fis);
while(st.nextToken()!=StreamTokenizer.TT_EOF) {
String s;
switch(st.ttype) {
case StreamTokenizer.TT_NUMBER: s = st.nval+"";
break;
case StreamTokenizer.TT_WORD: s = st.sval;
break;
default: s = ""+((char)st.ttype);
}
map.put(i+"",s);
set.add(s);
i++;
}
Iterator iter = set.iterator();
System.out.println("Frequency Of Words :");
while(iter.hasNext()) {
String word;
int count=0;
word=(String)iter.next();
for(int j=0; j<i ; j++) {
String word2;
word2=(String)map.get(j+"");
if(word.equals(word2))
count++;
}
System.out.println(" WORD : "+ word+" = "+count);
}
System.out.println("Total Words In Files: "+i);
}
}
在此代码中,首先我已经创建了一个包含以下数据的文本文件:
@Hello Hii World#* c++ java salesforce
这段代码的输出是:
**词频:
字:# = 1
字:@ = 1
字:c = 1
字:销售人员 = 1
字:* = 1
字:这= 1
字:+ = 2
字:java = 1
字:世界 = 1
单词:你好 = 1
文件总字数:11**
我无法找到为什么这将 c++ 显示为单独的单词。我想在输出中将 c++ 组合为一个单词
holdtom
相关分类