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);
}
}
}
}
代码功能:
- 从文本文件 "1.txt" 中读取内容,并使用
StringOperation类中的split方法将文本内容分割成单词数组。 - 使用
capitalCharStringArray方法提取所有以大写字母开头的单词(视为人物姓名)。 - 将提取到的姓名存储到
Name类的数组中,并统计每个姓名的出现次数。 - 使用
bubbleSort方法对Name数组进行冒泡排序,按照出现次数从高到低排序。 - 输出前 20 名人物姓名及其出现次数。
注意:
- 代码中的文件路径 "D:\学校\课程内容\Java\Chapter10_NumOfName\src\1.txt" 需要根据实际情况进行修改。
- 代码使用了冒泡排序,效率较低,可以考虑使用更高效的排序算法,例如快速排序或归并排序。
- 代码中对文本内容的处理较为简单,可以根据实际需要进行调整,例如可以添加对特殊符号或空格的处理。
原文地址: https://www.cveoy.top/t/topic/oEmx 著作权归作者所有。请勿转载和采集!