zoukankan      html  css  js  c++  java
  • 作业

    package pinlv;
    import java.io.BufferedReader;
    import java.io.FileReader;
    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 Pinlv {
        public static void main(String[] args) throws Exception {
            BufferedReader reader = new BufferedReader(new FileReader("D:\wenzhang.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;
            for (int i = last; i > last - 6; i--) {
                String key = list.get(i).getKey();
                Integer value = list.get(i).getValue();
                System.out.println(key + " :" + value);
            }
        }
    }

    在上机的前半个多小时里,我终于弄明白了怎么读取txt文件,在接下来的编程中使我的效率大大地提高,然后就是主要卡在了不知该怎么比较单词出现的频率

  • 相关阅读:
    阿里云Centos7安装和启动nginx
    mysql安装配置、主从复制配置详解
    php 文件大小计算转换
    tp5.0 生成二维码 + 合并海报图
    iTerm2 + Oh My Zsh
    linux 安装 卸载 命令
    php两个时间日期相隔的天数,时,分,秒.
    PhpStorm下载 + 破解
    php 地区三级联动
    PhpSpreadsheet 引入类库 导出 excel
  • 原文地址:https://www.cnblogs.com/baixiaoli/p/9775068.html
Copyright © 2011-2022 走看看