10/07/08 05:45:44
>>251
import java.util.Map;
import java.util.TreeMap;
public class SentenceAnalizer {
public static void main(String[] args) {
SentenceAnalizer analizer = new SentenceAnalizer();
String sentence = "hello world";
Map<Character, Long> map = analizer.analize(sentence);
for (Character c : map.keySet()) {
System.out.println(c + ":" + map.get(c));
}
}
public Map<Character, Long> analize(String str) {
Map<Character, Long> map = new TreeMap<Character, Long>();
char[] chars = str.toLowerCase().toCharArray();
for (char c : chars) {
if (map.containsKey(c)) map.put(c, map.get(c) + 1);
else map.put(c, new Long(1));
}
return map;
}
}