【java】统计一篇英文文章每个字母出现的次数

try {
			FileReader file = new FileReader("D:/temp/data.xml");
			BufferedReader fr = new BufferedReader(file);
			try {
				String str="";
					str=fr.readLine();
					char[] array = str.toCharArray();
					TreeMap<Character, Integer> map= new TreeMap<Character, Integer>();
					for (int i = 0; i < array.length; i++) {
						array[i] = Character.toLowerCase(array[i]);
						if (array.length>1) {
							if (map.get(array[i])==null) {
								map.put(array[i], 1);
							}else {
								int value = map.get(array[i]).intValue();
								value++;
								map.put(array[i], value);								
							}							
						}						
					}
					for(Map.Entry<Character, Integer> entry: map.entrySet()) {
						 System.out.print(entry.getKey() + "--" + entry.getValue() + "\n");
						}
				fr.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}


你可能感兴趣的:(【java】统计一篇英文文章每个字母出现的次数)