题目:取出一个字符串中字母出现的次数。如:字符串:"abcdekka27qoq" ,输出格式为: a(2)b(1)k(2)...
import java.util.Scanner;
import java.util.TreeMap;
public class Test1 {
/**
* @param args
*/
public static void main(String[] args) {
String str = getStr();
TreeMap<Character, Integer> tm = new TreeMap<>();
char[] arr = str.toCharArray();
for (char c : arr) {
if (!tm.containsKey(c)) {
tm.put(c, 1);
}else {
tm.put(c, tm.get(c) + 1);
}
}
for (Character c1 : tm.keySet()) {
Integer value = tm.get(c1);
System.out.print(c1 + "(" + value + ")");
}
}
private static String getStr() {
Scanner sc = new Scanner(System.in);;
String str = sc.nextLine();
return str;
}
}
|
|