取出一个字符串中字母出现的次数。如:字符串:"abcdekka27qoq",输出格式为:a(2)b(1)k(2)...
并把输出的结果,写到txt文档中
* @throws IOException
**
*/
public static void main(String[] args) throws IOException {
String s1="abcdekka27qoq4234gfg";
char[]crr=s1.toCharArray();
HashMap<Character, Integer>h1=new HashMap<>();
for(int i=0;i<crr.length;i++){
if(!(h1.containsKey(crr[i]))){
h1.put(crr[i], 1);
}
else{
h1.put(crr[i], h1.get(crr[i])+1);
}
}
FileOutputStream f=new FileOutputStream("aa.txt");
for(Character c2:h1.keySet()){
f.write((c2+"("+h1.get(c2)+")").getBytes());
}
}
}
|
|