2020-09-29日报博客-周二

1.学到的东西:

package Test2;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.*;

public class WordReader {
    private static int N = 10;

    public static void main(String[] args) throws FileNotFoundException {
        File file = new File("/home/long/Documents/a.txt");
        Scanner scanner = new Scanner(file);
        List<Map.Entry<String, Integer>> list = new ArrayList<>();
        Map<String, Integer> map = new HashMap<>();
        while (scanner.hasNext()) {
            boolean ishave = false;
            String word = scanner.next();
            for (Map.Entry<String, Integer> pair : map.entrySet()) {
                if (word.equalsIgnoreCase(pair.getKey())) {
                    ishave = true;
                    int count = pair.getValue() + 1;
                    pair.setValue(count);
                }
            }
            if (!ishave) {
                map.put(word.toLowerCase(), 1);
            }
        }
        for (Map.Entry<String, Integer> pair : map.entrySet()) {
            list.add(pair);
        }
        list = sort(list);
        for (int i = 0; i < list.size(); i++) {
            System.out.println(list.get(i));
        }
    }

    public static List<Map.Entry<String, Integer>> sort(List<Map.Entry<String, Integer>> list) {
        for (int i = 0; i < list.size(); i++) {
            for (int j = 0; j < list.size() - 1 - i; j++)
                if (list.get(j + 1).getValue() > list.get(j).getValue()) {
                    Map.Entry<String, Integer> temp = list.get(i);
                    list.set(j + 1,list.get(j));
                    list.set(j,temp);
                }
        }
        return list;
    }

    public static void exchange(Map.Entry<String, Integer> a,Map.Entry<String, Integer> b){
        String tempStr;
        int tempInt;
        tempStr = a.getKey();
        tempInt = a.getValue();
    }
}

2.遇到的问题:读取文件时,如何处理非法的字符格式。

3.明日计划:继续学习Java

posted @ 2020-09-29 20:50  巩云龙  阅读(45)  评论(0编辑  收藏  举报