1. 程式人生 > 實用技巧 >java--遍歷字元個數

java--遍歷字元個數

package test_1027;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Scanner;
import java.util.TreeMap;

public class ioStream {
public static void main(String[] args) throws IOException {
//FileInputStream
//method_1();
//method_2();
//method_3(fis, fos);
//BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
//extracted1();
//person p =new person(new Student1());
//p.code();
Scanner sc =new Scanner(System.in);
FileOutputStream fos =new FileOutputStream("yyy.txt",true);

//System.out.println();
while(true) {
String s= sc.nextLine();
char[] arr= s.toCharArray();
HashMap<Character, Integer> hm =new HashMap();
for (char c : arr) {
hm.put(c, !hm.containsKey(c) ? 1:hm.get(c)+1);
}
System.out.println(hm);
for (Character c : hm.keySet()) {
System.out.println(c+"---"+hm.get(c));
}
if(s.equals("q")){
break;
}

			fos.write(s.getBytes());
			fos.write("\r\n".getBytes());
	}
	fos.close();
	
}

}