计算最长英语单词链

 

我的思路就是:先从input1里面读取文件,sb1.append(s1);方法逐行读取文件内容,不读取换行符和末尾的空格,将读取的字符串添加换行符后累加p存放在缓存中。接着将特殊符号,;.等用空格代替text.replace(array[j]," ");方法,然后用split方法按空格分割放到一个数组里。然后用subSequence方法取单词首字母,尾字母,两个for循环,如果前一个单词的尾字母等于后一个单词的首字母,用一个字符串连接起来这两个单词(空格分开),最后与一开始假设的最长单词链max比较,长的话则代替成为新的max,就这样循环找到最长单词链。

package test;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class Dragon {
    public static void main(String[] args) throws IOException {
         jieD();
        /*Read_File();*/
    }
    public static String[] Read_File()throws IOException {
    File file1 = new File("input1.txt");//定义一个file对象,用来初始化FileReader
    if (!file1.exists()) {// 如果文件打不开或不存在则提示错误
        System.out.println("您的文件不存在");
    }else {
        if(file1.exists() && file1.length() == 0) {  
            System.out.println("文件为空!");  
        }  
    }
    FileReader reader1 = new FileReader(file1);
    BufferedReader br=new BufferedReader(reader1);
    StringBuilder sb1 = new StringBuilder();//定义一个字符串缓存,将字符串存放缓存中
    String s1 = "";
    while ((s1 =br.readLine()) != null) {//逐行读取文件内容,不读取换行符和末尾的空格
    sb1.append(s1);//将读取的字符串添加换行符后累加p存放在缓存中
    }
    br.close();
    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(" ");  
    return textArray;
    /*for(int i=0;i<textArray.length;i++) {
        System.out.println(textArray[i]+" ");
    }*/    
    }
    public static void writeFile(String max) {
        
        File f=new File("output1.txt");
        if(!f.exists()) {
            try {
                f.createNewFile();
                System.out.println("output1.txt创建完成");
            } catch (IOException e) {
                // TODO 自动生成的 catch 块
                e.printStackTrace();
                System.out.println("output1.txt创建失败");
            }            
        }    
        FileWriter fw;
        try {
            fw = new FileWriter(f);
        
        BufferedWriter bw=new BufferedWriter(fw);                
        bw.write(max);
        bw.close();
        fw.close();        
        } catch (IOException e) {
            // TODO 自动生成的 catch 块
            e.printStackTrace();
        }
}
    public static void jieD() throws IOException {
        String textArray[]=Read_File();
//        int a[]=new int[textArray.length];
//        for(int k=0;k<textArray.length;k++) {
//            a[k]=0;
//        }
        for(int i=0;i<textArray.length;i++) {
            System.out.println(textArray[i]);
        }
        String str="";
        String max="";
        String word="";
        for(int i=0;i<textArray.length;i++) {
            if(!(textArray[i].equals(" "))) {
             str=textArray[i];
             word=str;/*前一个单词*/
            /*char s=textArray[i].charAt(0);*/
            /*System.out.println(s);*/
             for(int j=i+1;j<textArray.length;j++) {
                 if(!(textArray[j].equals(" "))) {
                    if(textArray[j].toLowerCase().subSequence(0, 1).equals(word.toLowerCase().subSequence(word.length()-1, word.length()))) {/*toLowerCase() 方法用于把字符串转换为小写。*/
                        word = textArray[j];
                        str+=" "+word;
                    }
             }
             }

                if(str.indexOf(" ")!=-1) {/*public int indexOf(int ch): 返回指定字符在字符串中第一次出现处的索引,如果此字符串中没有这样的字符,则返回 -1。*/
                    if(str.length()>max.length()) {
                        max = str;
                    }
//                    System.out.println(str);
                }
        }
        }
        /*System.out.println(max);*/
        writeFile(max);
                
            /*char m=textArray[i].charAt(str.length()-1);*/
                /*for(int j=i+1;j<textArray.length;j++) {
                char s1=textArray[j].charAt(0);
                char m1=textArray[j].charAt(str.length()-1);
                if(m==s1) {
                    System.out.println(str);
                    m=m1;
                }
            }*/
    }    
}

截图:

input1.txt

output1.txt:

 

posted @ 2019-06-09 14:22  云破月来花弄影  阅读(153)  评论(0编辑  收藏  举报