Java程序统计文件中的大写字母单词出现次数并排序
package Chapter10_NumOfName;
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 firstCharIsCapital(String s) { // 将方法名改为firstCharIsCapital
if (s.isEmpty()) {
return false;
}
return Character.isUpperCase(s.charAt(0));
}
public static String[] capitalCharStringArray(String[] s) {
String[] capitalStringArray = new String[10];
int size = 0;
for (String str : s) {
if (firstCharIsCapital(str)) {
if (size >= capitalStringArray.length) {
String[] tempString = new String[capitalStringArray.length * 2];
System.arraycopy(capitalStringArray, 0, tempString, 0, capitalStringArray.length);
capitalStringArray = tempString;
}
capitalStringArray[size] = str;
size++;
}
}
return capitalStringArray;
}
public static Name[] bubbleSort(Name[] names) {
for (int i = 0; i < names.length; i++) {
for (int j = i + 1; j < names.length; j++) { // 将第二个循环的i改为j
if (names[i].count < names[j].count) {
Name temp = names[i];
names[i] = names[j];
names[j] = temp;
}
}
}
return names;
}
}
package Chapter10_NumOfName;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class FileOperation {
public static String readFromFile(File f) throws FileNotFoundException {
Scanner input = new Scanner(f);
StringBuilder result = new StringBuilder(); // 使用StringBuilder来拼接字符串
while (input.hasNextLine()) {
result.append(input.nextLine()).append("\n"); // 每行读入后加上换行符
}
input.close();
return result.toString();
}
public static void writeToFile(File f, String[] s) throws FileNotFoundException {
try (PrintWriter output = new PrintWriter(f)) { // 使用try-with-resources语句
for (String str : s) {
if (!str.isEmpty()) {
output.println(str);
}
}
}
}
}
package Chapter10_NumOfName;
public class Name {
private String name;
private int count;
public Name(String name, int count) {
this.name = name;
this.count = count;
}
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;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Arrays;
public class Test2 {
public static void main(String[] args) throws FileNotFoundException {
File inputFile = new File("1.txt");
String fileContent = FileOperation.readFromFile(inputFile);
String[] words = StringOperation.split(fileContent);
String[] capitalWords = StringOperation.capitalCharStringArray(words);
File outputFile = new File("2.txt");
FileOperation.writeToFile(outputFile, capitalWords);
Name[] names = new Name[capitalWords.length];
int index = 0;
for (String word : capitalWords) {
if (!word.isEmpty()) {
boolean flag = false;
int k = 0;
for (int j = 0; j < index; j++) {
if (names[j].getName().equals(word)) {
flag = true;
k = j;
break;
}
}
if (flag) {
names[k].increaseCount();
} else {
names[index] = new Name(word, 1);
index++;
}
}
}
names = StringOperation.bubbleSort(Arrays.copyOf(names, index));
for (int i = 0; i < 20 && i < names.length && names[i].getCount() != 0; i++) {
System.out.println(names[i].getName() + " " + names[i].getCount());
}
}
}
原文地址: https://www.cveoy.top/t/topic/oEmJ 著作权归作者所有。请勿转载和采集!