题目:
第0步:输出某个英文文本文件中 26 字母出现的频率,由高到低排列,并显示字母出现的百分比,精确到小数点后面两位。
第1步:输出单个文件中的前 N 个最常出现的英语单词。作用:一个用于统计文本文件中的英语单词出现频率。
设计思想:首先是统计字母,我们应该先把要统计的文件读取,遍历统计字母出现的次数,将大写字母转换为小写字母;统计单词也需要将大写字母转换为小写,只要遇到空格则记为一个单词,遍历一遍统计单词个数。
遇到的问题:不知道该如何显示百分比,后来在网上查找发现可以用formattedDecimalToPercentage这个函数来实现
package test;import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;public class zimu {private static String formattedDecimalToPercentage(double decimal){//获取格式化对象NumberFormat nt = NumberFormat.getPercentInstance();//设置百分数精确度2即保留两位小数nt.setMinimumFractionDigits(2);return nt.format(decimal);}
public static void main(String []args) {String a1;char a='A';int a2[]=new int[27];char b1[]=new char[26];char b2[]=new char[26];double c1[]=new double[26];for(int i=0;i<26;i++){b1[i]=a;b2[i]=(char)(a+32);a++;}try {BufferedReader in = new BufferedReader(new FileReader("D:\\piao.txt"));String str;while ((str = in.readLine()) != null) {char[] d=str.toCharArray();for(int i=0;i<d.length-1;i++) {for(int j=0;j<26;j++) {if(b1[j]==d[i]||b2[j]==d[i]) {a2[j]++;}}}}a2[26]=0;for(int i=0;i<26;i++) {a2[26]=a2[i]+a2[26];}for(int i=0;i<26;i++) {c1[i]=(double)((double)a2[i]/(double)a2[26]);}for(int i=0;i<26;i++) {System.out.print(b1[i]);System.out.print("和");System.out.print(b2[i]);System.out.print("出现的次数为:");System.out.print(a2[i]);double d=(double)((double)a2[i]/(double)a2[26]);String result2=formattedDecimalToPercentage(d);System.out.println(" 百分比为:"+result2);}System.out.println(" ");System.out.println("出现单词次数较多的前十个为:");BufferedReader reader = new BufferedReader(new FileReader("D:\\\\piao.txt"));StringBuffer buffer = new StringBuffer();String line = null;while ((line = reader.readLine()) != null) {buffer.append(line);}reader.close();Pattern expression = Pattern.compile("[a-zA-Z]+");String string = buffer.toString();Matcher matcher = expression.matcher(string);//Map<String, Integer> map = new TreeMap<String, Integer>();String word = "";int times = 0;while (matcher.find()) {word = matcher.group();if (map.containsKey(word)) {times = map.get(word);map.put(word, times + 1);} else {map.put(word, 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);// 排序int last = list.size() - 1;int asdad=0;for(int i=last;i>0;i--) {String key = list.get(i).getKey();Integer value = list.get(i).getValue();asdad=asdad+value;}for (int i = last; i > last - 10; i--) {String key = list.get(i).getKey();Integer value = list.get(i).getValue();System.out.print(key + " :" + value);double d=(double)((double)value/(double)asdad);String result2=formattedDecimalToPercentage(d);System.out.println(" 百分比为:"+result2);}} catch (IOException e) {}
}
}
结果截图:
总结:这次课上的作业让我对文件的读取有了进一步的掌握,文件的读取已经做过很多次了,但是自己还不是很了解,应该多加练习,formattedDecimalToPercentage这个函数可以用来小数格式化和百分比的显示