课程九,课堂测试
package harrypoter; import java.io.File; import java.io.FileReader; import java.io.IOException; import java.text.DecimalFormat; public class first{ public static void main(String[] args) { double num=0; int[] freArray = new int[52]; char[] charArray = loadCharArrayFromFileName("C:\\Users\\凋零\\Desktop\\Harry Potter and the Sorcerer's Stone.txt"); computeFrequency(freArray, charArray); for(int i = 0; i < freArray.length; i++) { num+=freArray[i]; } System.out.println(num); DecimalFormat df = new DecimalFormat("######0.00"); int a=freArray[0]; for (int i = 0; i < freArray.length; i++) { System.out.println(((char) ('a' + i)) + ":" + freArray[i]+" "+df.format((100*freArray[i])/num)+"%"); } for (int i = 26; i < freArray.length; i++) { System.out.println(((char) ('A' + i-26)) + ":" + freArray[i]+" "+df.format((100*freArray[i])/num)+"%"); } } public static void computeFrequency(int[] freArray, char[] charArray) { for (int i = 0; i < charArray.length; i++) { if (charArray[i] >= 'A' && charArray[i] <= 'Z') { freArray[charArray[i] - 'A'+26]++; } if (charArray[i] >= 'a' && charArray[i] <= 'z') { freArray[charArray[i] - 'a']++; } } } public static char[] loadCharArrayFromFileName(String name) { char[] charArray = new char[5000000]; File file = new File(name); FileReader fr = null; try { fr = new FileReader(file); fr.read(charArray); return charArray; } catch (IOException e) { e.printStackTrace(); } finally { try { fr.close(); } catch (IOException e) { e.printStackTrace(); } } return null; } }
测试二:
package harrypoter; import java.io.BufferedReader; import java.io.FileReader; import java.util.*; public class second { public static void main(String [] args) throws Exception { BufferedReader br = new BufferedReader(new FileReader("C:\\Users\\凋零\\Desktop\\Harry Potter and the Sorcerer's Stone.txt")); StringBuffer sb = new StringBuffer(); String text =null; while ((text=br.readLine())!= null){ sb.append(text); } br.close(); // 关闭读入流 String str = sb.toString().toLowerCase(); // 将stringBuffer转为字符并转换为小写 String[] words = str.split("[^(a-zA-Z)]+"); // 非单词的字符来分割,得到所有单词 Map<String ,Integer> map = new HashMap<String, Integer>() ; for(String word :words){ if(map.get(word)==null){ map.put(word,1); }else{ map.put(word,map.get(word)+1); // 若存在,次数累加1 } } // 排序 List<Map.Entry<String ,Integer>> list = new ArrayList<Map.Entry<String,Integer>>(map.entrySet()); Comparator<Map.Entry<String,Integer>> comparator = new Comparator<Map.Entry<String, Integer>>() { public int compare(Map.Entry<String, Integer> left, Map.Entry<String, Integer> right) { return (left.getValue().compareTo(right.getValue())); } }; // 集合默认升序升序 Collections.sort(list,comparator); for(int i=0;i<list.size();i++){// 由高到低输出 System.out.println(list.get(list.size()-i-1).getKey() +":"+list.get(list.size()-i-1).getValue()); } } }
测试三:
package harrypoter; import java.io.BufferedReader; import java.io.FileReader; import java.util.*; public class second { public static void main(String [] args) throws Exception { BufferedReader br = new BufferedReader(new FileReader("C:\\Users\\凋零\\Desktop\\Harry Potter and the Sorcerer's Stone.txt")); StringBuffer sb = new StringBuffer(); String text =null; while ((text=br.readLine())!= null){ sb.append(text);// 将读取出的字符追加到stringbuffer中 } br.close(); // 关闭读入流 String str = sb.toString().toLowerCase(); // 将stringBuffer转为字符并转换为小写 String[] words = str.split("[^(a-zA-Z)]+"); // 非单词的字符来分割,得到所有单词 Map<String ,Integer> map = new HashMap<String, Integer>() ; for(String word :words){ if(map.get(word)==null){ // 若不存在说明是第一次,则加入到map,出现次数为1 map.put(word,1); }else{ map.put(word,map.get(word)+1); // 若存在,次数累加1 } } // 排序 List<Map.Entry<String ,Integer>> list = new ArrayList<Map.Entry<String,Integer>>(map.entrySet()); Comparator<Map.Entry<String,Integer>> comparator = new Comparator<Map.Entry<String, Integer>>() { public int compare(Map.Entry<String, Integer> left, Map.Entry<String, Integer> right) { return (left.getValue().compareTo(right.getValue())); } }; // 集合默认升序升序 Collections.sort(list,comparator); for(int i=0;i<list.size();i++){// 由高到低输出 System.out.println(list.get(list.size()-i-1).getKey() +":"+list.get(list.size()-i-1).getValue()); } } }