package Chapter10_NumOfName;
// 许家玮 1925123026 软件工程三班

import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.Scanner;

public class StringOperation {
    public static String[] split(String s) {
        String afterOperation = eraseMeaninglessWord(s);
        return afterOperation.split(" ");
    }

    public static String eraseMeaninglessWord(String s) {
        s = s.replace("Mr.", " ");
        s = s.replace("Mrs.", " ");
        s = s.replace('' + "m", " ");
        s = s.replace("I", " ");
        s = s.replace("", " ");
        s = s.replace(''', " ");
        s = s.replace(".", " ");
        s = s.replace(".'" + '"', " ");
        s = s.replace('"', " ");
        s = s.replace(",", " ");
        s = s.replace(";", " ");
        s = s.replace("?", " ");
        s = s.replace("!", " ");
        return s;
    }

    public static boolean firstCharIsCaptal(String s) {
        boolean result = false;
        if (s.equals("")) {
            return false;
        }
        if (Character.isUpperCase(s.charAt(0))) {
            result = true;
        } else {
            result = false;
        }
        return result;
    }

    public static String[] capitalCharStringArray(String[] s) {
        String[] capitalStringArray = new String[10];
        int size = 0;
        int count = 0;
        while (count < s.length) {
            if (firstCharIsCaptal(s[count])) {
                if (size < capitalStringArray.length) {
                    capitalStringArray[size] = s[count];
                } else {
                    String[] tempString = new String[capitalStringArray.length * 2];
                    for (int i = 0; i < tempString.length; i++) {
                        tempString[i] = "";
                    }
                    for (int i = 0; i < capitalStringArray.length; i++) {
                        tempString[i] = capitalStringArray[i];
                    }
                    capitalStringArray = tempString;
                    tempString[size] = s[count];
                }
                size = size + 1;
            }
            count = count + 1;
        }
        return capitalStringArray;
    }

    public static Name[] bubbleSort(Name[] names) {
        for (int i = 0; i < names.length; i++) {
            for (int j = i; j < names.length; i++) {
                if (names[i].count < names[j].count) {
                    Name temp = names[i];
                    names[i] = names[j];
                    names[j] = temp;
                }
            }
        }
        return names;
    }
}
package Chapter10_NumOfName;
// 许家玮 1925123026 软件工程三班

import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.Scanner;

public class FileOperation {
    public static String readFromFile(File f) throws FileNotFoundException {
        Scanner input = new Scanner(f);
        String result = "";
        while (input.hasNextLine()) {
            result = result + input.hasNextLine();
        }
        input.close();
        return result;
    }

    public static void writeToFile(File f, String[] s) throws FileNotFoundException {
        PrintWriter output = new PrintWriter(f);
        for (int i = 0; i < s.length; i++) {
            if (s[i].equals("")) {
                break;
            } else {
                output.println(s[i]);
            }
        }
        output.close();
    }
}
package Chapter10_NumOfName;
// 许家玮 1925123026 软件工程三班

public class Name {
    String name;
    int count;

    public Name() {
        name = "";
        count = 0;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getCount() {
        return count;
    }

    public void setCount(int count) {
        this.count = count;
    }

    public void increaseCount() {
        this.count++;
    }
}
package Chapter10_NumOfName;
// 许家玮 1925123026 软件工程三班
import java.io.File;
import java.io.FileNotFoundException;

public class Test2 {
    public static void main(String[] args) throws FileNotFoundException {
        File f = new File("D:\学校\课程内容\Java\Chapter10_NumOfName\src\1.txt");
        String reaFileContent = Chapter10_NumOfName.FileOperation.readFromFile(f);
        System.out.println(reaFileContent);
        String[] arrayfromString = Chapter10_NumOfName.StringOperation.split(reaFileContent);
        String[] stringWriteToFile = Chapter10_NumOfName.StringOperation.capitalCharStringArray(arrayfromString);

        File output = new File("2.txt");
        FileOperation.writeToFile(output, stringWriteToFile);

        Name[] names = new Name[stringWriteToFile.length];

        for (int i = 0; i < names.length; i++) {
            names[i] = new Name();
        }

        int index = 0;

        for (int i = 0; i < stringWriteToFile.length; i++) {
            if (!stringWriteToFile[i].isEmpty()) {
                boolean flag = false;
                int k = 0;
                for (int j = 0; j < index; j++) {
                    if (names[j].name.equals(stringWriteToFile[i])) {
                        flag = true;
                        k = j;
                        break;
                    }
                }
                if (flag) {
                    names[k].increaseCount();
                } else {
                    index++;
                    names[index].setName(stringWriteToFile[i]);
                    names[index].setCount(1);
                }
            }
        }

        names = Chapter10_NumOfName.StringOperation.bubbleSort(names);

        for (int i = 0; i < 20; i++) {
            if (names[i].count != 0) {
                System.out.println(names[i].name + " " + names[i].count);
            }
        }
    }
}

代码说明:

  1. 文件读取: FileOperation.readFromFile(f) 方法读取文件内容,并返回一个字符串。
  2. 字符串处理: StringOperation.split(reaFileContent) 方法将字符串按空格拆分成单词数组,StringOperation.capitalCharStringArray(arrayfromString) 方法提取首字母大写的单词,即可能的人名。
  3. 人名统计: 代码使用一个 Name[] 数组存储人名和出现次数,遍历提取出的单词,如果该单词已存在于 names 数组中,则增加该单词的计数,否则将该单词加入 names 数组并初始化计数为 1。
  4. 排序: StringOperation.bubbleSort(names) 方法使用冒泡排序算法,按照出现次数从高到低排序 names 数组。
  5. 输出: 代码输出排序后前 20 个出现次数最多的名字及其出现次数。

代码改进建议:

  1. 使用更高级的排序算法,例如快速排序或归并排序,可以提高效率。
  2. 使用 HashMap 或其他数据结构存储人名和计数,可以提高统计效率。
  3. 添加对人名规范化的处理,例如去除前后空格、统一大小写等。
  4. 可以根据实际需求,调整输出的人名数量。

使用方法:

  1. 将代码保存为 Java 文件,例如 NameCounter.java
  2. Test2.java 文件中修改 File f 的路径,指向要统计的人名文本文件。
  3. 编译并运行代码,即可在控制台中看到统计结果。

注意:

本代码示例仅供参考,实际应用中可能需要根据具体情况进行修改和调整。

Java 文件人名统计与排序 - 统计文本文件中人名出现次数并排序输出

原文地址: https://www.cveoy.top/t/topic/oEms 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录