Java程序:统计文本文件中人名出现次数并排序输出
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.nextLine();
}
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);
}
}
}
}
这段代码首先读取文本文件内容,然后使用 StringOperation 类中的 split 方法将文本内容按空格分割成单词数组。接着,使用 capitalCharStringArray 方法筛选出首字母大写的单词(可能的人名)。
接下来,程序创建一个 Name 数组,用于存储人名及其出现次数。遍历筛选后的单词数组,如果该单词在 Name 数组中已经存在,则增加其计数;否则,将该单词添加到数组中,并将计数设置为 1。
最后,使用 bubbleSort 方法对 Name 数组按照出现次数进行降序排序,并输出前 20 个出现次数最高的人名及其出现次数。
注意:
- 代码中的
D:\\学校\\课程内容\\Java\\Chapter10_NumOfName\\src\\1.txt是文本文件路径,需要根据实际情况进行修改。 - 代码中的
2.txt是输出文件路径,需要根据实际情况进行修改。 - 代码中的
20是输出人名的数量,可以根据需要进行修改。
示例:
假设文本文件 1.txt 内容如下:
Mr. John Smith is a software engineer. He works for Google. Mrs. Jane Doe is a marketing manager. She also works for Google.
运行代码后,程序会输出以下结果:
John 2
Jane 1
Smith 1
Google 2
改进建议:
- 可以使用更高级的排序算法,例如快速排序或归并排序,提高排序效率。
- 可以使用正则表达式,更精确地匹配人名。
- 可以使用哈希表来存储人名及其出现次数,进一步提高代码效率。
希望这段代码和解释能够帮助您理解如何使用 Java 统计文本文件中人名出现的次数并进行排序输出。
原文地址: https://www.cveoy.top/t/topic/oEmt 著作权归作者所有。请勿转载和采集!