★★ Java の宿題ここで答えます Part 52 ★★

このエントリーをはてなブックマークに追加
187デフォルトの名無しさん
>>183
適当に書いてみた
import java.io.*;
import java.util.*;
import java.util.regex.*;

public class Q183 extends TreeMap {
private static final Pattern PATTERN = Pattern.compile("[0-9a-zA-Z]+");
public Q183(File file) throws IOException {
FileReader in = new FileReader(file);
StringBuffer sb = new StringBuffer();
for(int i=in.read(); i != -1; i=in.read()) sb.append((char)i);
in.close();
Matcher matcher = PATTERN.matcher(sb);
while(matcher.find()) {
String key = matcher.group().toLowerCase();
if(containsKey(key))
put(key, new Integer(((Integer)get(key)).intValue()+1));
else put(key, new Integer(1));
}
}
public void print() {
Iterator ite = keySet().iterator();
while(ite.hasNext()) {
String key = (String)ite.next();
System.out.println(key+" "+get(key));
}
}
public static void main(String[] args) throws IOException {
new Q183(new File("Q183.java")).print();
}
}