编程语言
首页 > 编程语言> > Java学习-动手动脑-Java流

Java学习-动手动脑-Java流

作者:互联网

Java流学习,读取文件并按要求输出

阶段一:按由大到小的顺序输出每个字母出现的次数和概率

阶段二:由大到小输出前N个单词出现的次数和概率

阶段三:输出单词出现的次数和概率不包括“a" "the" "of"

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.text.NumberFormat;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class main2 {

    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("E:\\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("       ");
            BufferedReader reader = new BufferedReader(new FileReader(
                    "E:\\\\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;

            }


            /*System.out.println("请输入你想显示的排名前多少个单词:");
            int N;
            Scanner scan=new Scanner(System.in);
            N=scan.nextInt();


            for (int i = last; i > last - N; i--)  //阶段二
            {
                String key = list.get(i).getKey();

                Integer value = list.get(i).getValue();
                System.out.println(key + " :" + value);
                double d=(double)((double)value/(double)asdad);
                String result2=formattedDecimalToPercentage(d);
                System.out.println("   百分比为:"+result2);
            }*/
            /*for (int i = last; i >0; i--)    //阶段三
            {
                String key = list.get(i).getKey();
                if(key.equals("a")||key.equals("the")||key.equals("of"))  continue;
                Integer value = list.get(i).getValue();
                System.out.println(key + " :" + value);
            }*/
        for (int i = last; i >0; i--)
        {
            String key = list.get(i).getKey();
            if(key.equals("a")||key.equals("the")||key.equals("of"))  continue;
            Integer value = list.get(i).getValue();
            System.out.println(key + " :" + value);
          /* double d=(double)((double)value/(double)asdad);
           String result2=formattedDecimalToPercentage(d);
        System.out.println("   百分比为:"+result2);*/
        }

        } catch (IOException e) {
        }
    }
}

  不同阶段运行时删掉注释符即可

标签:Java,String,int,double,动脑,list,value,动手,key
来源: https://www.cnblogs.com/333czh/p/15546387.html