2020-10-4日报博客-周日总结

1.学到的东西:

package Test1;

import java.io.*;
import java.text.DecimalFormat;
import java.util.HashMap;
import java.util.Map;

public class TextReader {
    public static void main(String[] args) throws IOException {
        File file = new File("/home/long/Documents/Piao.txt");
        FileReader fileReader = new FileReader(file);
        int b;
        int[] count = new int[52];
        for (int i = 0; i < count.length; i++) {
            count[i] = 0;
        }
        char[] chars = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
                'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'};
        while ((b = fileReader.read()) != -1) {
            for (int i = 0; i < chars.length; i++) {
                if ((char) b == chars[i]) {
                    count[i]++;
                }
            }
        }
        int sunCunt = 0;
        for (int i = 0; i < count.length; i++) {
            sunCunt += count[i];
        }
        DecimalFormat df = new DecimalFormat("######0.00");
        for (int i = 0; i < count.length; i++) {
            System.out.println(chars[i] + ":"+df.format(((double)count[i]/(double)sunCunt) *100) + "%");
        }
    }
}

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

2.遇到的问题:

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

posted @ 2020-10-04 20:46  巩云龙  阅读(50)  评论(0编辑  收藏  举报