java中最长的词_Java——计算最长英语单词链

该Java程序旨在找出文本中最长的英语单词链,通过移除标点符号并比较单词的首尾字母来构建单词链。它首先读取文本文件,然后检查文件是否存在且不为空,接着找出最长单词的数量,并找到所有单词及其首尾字母。最后,程序在输出文件中记录下首尾相连的最长单词链。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

packagelongword;importjava.io.BufferedReader;importjava.io.File;importjava.io.FileNotFoundException;importjava.io.FileReader;importjava.io.FileWriter;importjava.io.IOException;public classMain {public static intfindwordlong(String text){

String[] array= {".",",","?","!"};for (int i1 = 0; i1 < array.length; i1++) {

text= text.replace(array[i1]," ");

}

String[] textArray= text.split(" ");returntextArray.length;

}public staticString[] findword(String text){

String[] array= {".",",","?","!","“","”"," "};for (int i1 = 0; i1 < array.length; i1++) {

text= text.replace(array[i1]," ");

}

String[] textArray= text.split(" ");returntextArray;

}public static String readtxt(String txt) throwsIOException

{

File file= new File(txt);//定义一个file对象,用来初始化FileReader

FileReader reader = null;try{

reader= newFileReader(file);

}catch(FileNotFoundException e) {//TODO Auto-generated catch block

e.printStackTrace();

}//定义一个fileReader对象,用来初始化BufferedReader

BufferedReader bReader = new BufferedReader(reader);//new一个BufferedReader对象,将文件内容读取到缓存

StringBuilder sb = new StringBuilder();//定义一个字符串缓存,将字符串存放缓存中

String s = "";while ((s =bReader.readLine()) != null) {//逐行读取文件内容,不读取换行符和末尾的空格

sb.append(s);//将读取的字符串添加换行符后累加p存放在缓存中

}

bReader.close();

String str=sb.toString();returnstr;

}public static voidappendMethodB(String fileName, String content) {try{//打开一个写文件器,构造函数中的第二个参数true表示以追加形式写文件,如果为 true,则将字节写入文件末尾处,而不是写入文件开始处

FileWriter writer = new FileWriter(fileName, true);

writer.write(content);

writer.close();

}catch(IOException e) {

e.printStackTrace();

}

}public static boolean judeFileExists(String txt) throwsIOException {

File file= newFile(txt);if (!file.exists()) {

System.out.println("文件不存在!");return false;

}else if(file.length() == 0)

{

System.out.println("文件为空!");return false;

}

String str=readtxt("input.txt");if(findwordlong(str)==1)

{

System.out.println("文件只有一个单词!");return false;

}else{return true;

}

}public static void main(String[] args) throwsIOException {//TODO Auto-generated method stub

if(judeFileExists("input.txt"))

{

String str=readtxt("input.txt");inti;int l=findwordlong(str);

String []word=newString[l];char []first=new char[l];char []tail=new char[l];

word=findword(str);for(i=0;i

{if(word[i].length()!=0)

{

first[i]=word[i].charAt(0);

tail[i]=word[i].charAt(word[i].length()-1);

System.out.println(word[i]);

}

}

String stemp=word[0];

appendMethodB("output.txt", word[0]);for(i=1;i

{if(first[i]==stemp.charAt(stemp.length()-1))

{

stemp=stemp+"-"+word[i];

appendMethodB("output.txt", "-"+word[i]);

}

}if(stemp.equals(word[0]))

{

System.out.println("没有首尾相接的单词!");

}else{

System.out.println(stemp);

}

}

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值