晚上参加了挖财的笔试,把考试写的整理一下 给大家分享,代码实现很简单 希望对有需要的人的有所帮助。
主要是还要体会这一类题目的处理方式,代码如下:
public class CountWord{
public static void main(String[] args) {
File file = new File("F:"+File.separator+"test.txt");
countWord(file);
}
public static void countWord(File file){
try {
Map<String, Integer> map = new TreeMap<String, Integer>();
BufferedReader buf = new BufferedReader(new FileReader(file));
String temp = null;
while((temp = buf.readLine())!= null){
char[] cha = temp.trim().toCharArray();
StringBuffer word = new StringBuffer();
for (int i = 0; i < cha.length; i++) {
String str = String.valueOf(cha[i]);
//判断如果读到的字符是空格、逗号、句号就把buffer里面的单词保存到map中,并将buffer清空。
if(str.equals(" ")||str.equals(",")||str.equals(".")){
word.toString().trim();
if(map.containsKey(word.toString())){
map.put(word.toString().trim(), map.get(word.toString())+1);
}else{
map.put(word.toString().trim(), 1);
}
word.delete(0, word.length()); //一定记得清空StringBuffer
}else{
word.append(cha[i]);
}
}
}
buf.close();
System.out.println(map);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}