package com.heima.IO;
import java.io.IOException;
import java.util.Scanner;
import java.util.Set;
import java.util.TreeMap;
/*取出一个字符串中字母出现的次数。如:字符串:"abcdekka27qoq",输出格式为:a(2)b(1)k(2)...*/
public class Day1_ClassTest2 {
public static void main(String[] args) throws IOException {
Scanner sc = new Scanner(System.in);
String s = sc.nextLine();
char[] ch = s.toCharArray();
TreeMap<Character, Integer> tm = new TreeMap<>();
for (char c : ch) {
if((c>='A'&&c<='Z')||(c>='a'&&c<='z')){
if(tm.containsKey(c)){
tm.put(c, tm.get(c)+1);
}else{
tm.put(c, 1);
}
}
}
Set<Character> st = tm.keySet();
for (Character cha : st) {
System.out.print(cha+"("+tm.get(cha)+")");
}
}
} |