求最长的首尾相接的单词链思路;FileReader fr = new FileReader(f1);
BufferedReader bReader = new BufferedReader(fr);
while((hasRead=bReader.readLine()) != null){
sb1.append(hasRead);//将读取的字符串添加换行符后累加p存放在缓存
}text.split(" ");根据空格将其分割成一个个单词存放进字符串数组。并用自定义的方法toLowerCase(String str)将每一个单词都转化成小写,并统计每一个单词的首字母和尾字母存进head组数和wei数组。运用双层循环遍历,分别遍历出每个单词作为第一个单词时的最长的单词链,跟根据其中存在的“➕”来判断谁的链组成的单词最多来输出到output.txt文件中。
package 统计英文字母出现频率;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;
public class Dancilian {
public static String toLowerCase(String str) {//将字符串统一转化成小写的
char []StringArr = str.toCharArray();
for (int i = 0; i < StringArr.length; i++) {
StringArr[i] = Character.toLowerCase(StringArr[i]);
}
StringBuffer sb = new StringBuffer();
for(int i = 0;i < StringArr.length;i++) {
sb.append(StringArr[i]);
}
String str1 = sb.toString();
return str1;
}
public static int shuliang(String str) {//统计每个字符串中“➕”的数量,根据“➕”在判断其是由多少个单词相接而成的。
char []StringArr = str.toCharArray();
int num = 0;
for(int i = 0 ;i < StringArr.length;i++) {
if(StringArr[i] == '+') {
num++;
}
}
return num;
}
public static void FileCaozuo(String infilename,String outfilename) throws IOException {//对文件的操作。
try {
File f1 = new File(infilename);
if(!f1.exists()) //如果输入文件不存在那么就停止程序的运行
System.out.println("input.txt文件不存在!");
System.exit(0);
}
File f2 = new File(outfilename);
if(!f2.exists()) {//如果输出文件不存在,那么就创建一个新文件。
f2.createNewFile();
}
FileReader fr = new FileReader(f1);
FileWriter fw = new FileWriter(f2);
BufferedReader bReader = new BufferedReader(fr);
StringBuilder sb1 = new StringBuilder();//定义一个字符串缓存,将字符串存放缓存中
String s1 = "";
BufferedWriter bw = new BufferedWriter(fw);
String hasRead;
while((hasRead=bReader.readLine()) != null){
sb1.append(hasRead);//将读取的字符串添加换行符后累加p存放在缓存中
// bw.write(hasRead);
// bw.newLine();
}
String text = sb1.toString();
String[] array = {".",",","?","!",":","‘","’","“","”","—",";","-"};
for (int j = 0; j < array.length; j++) {
text = text.replace(array[j]," "); //将text中的array数组中包含的特殊字符用空格代替
}
String[] textArray = text.split(" ");
char[] head = new char[10];
char[] wei = new char[10];
String[] danci = new String[textArray.length];
for(int i = 0 ;i < textArray.length;i ++) {
danci[i] = toLowerCase(textArray[i]);
}
for(int i = 0 ; i < danci.length;i++) {
if(danci[i].length()==0) {
System.out.println("input.txt中无单词");
System.exit(0);
}
head[i] = danci[i].charAt(0);
wei[i] = danci[i].charAt(danci[i].length()-1);
}
String[] fin = new String[10];
for(int i = 0 ; i < wei.length; i ++) {
StringBuilder sb2 = new StringBuilder();//定义一个字符串缓存,将字符串存放缓存中
char c = wei[i];
sb2.append(danci[i]+"+");
for(int j = 0 ; j < head.length; j ++) {
if(head[j] ==wei[i]) {
wei[i] = wei[j];
sb2.append(danci[j]+"+");
}
}
String sb22 = sb2.toString();
fin[i] = sb22;
}
int max = shuliang(fin[0]);
int n = 0;
String maxf = fin[0];
for(int i = 1 ;i < fin.length;i++) {
n = shuliang(fin[i]);
if(n>max) {
maxf = fin[i];
}
}
if(n==1) {
System.out.println("无可连接单词");
System.exit(0);
}
bw.write(maxf);
bw.newLine();
bw.flush();
bReader.close();
} catch (FileNotFoundException e){
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String args[]) throws IOException {
Scanner scan = new Scanner(System.in);
System.out.println("请输入两个文件名,以.txt结尾!");
String file1 = scan.next();
String file2 = scan.next();
FileCaozuo(file1,file2);
scan.close();
运行截图:
input.txt
output.txt