十五周课堂练习-最长单词链

通过input.txt文件读取所有单词,将最长的单词接龙输出到out.txt文件。

  1 package text;
  2 
  3 import java.io.BufferedReader;
  4 import java.io.FileReader;
  5 import java.io.File;
  6 import java.io.FileInputStream;
  7 import java.io.FileNotFoundException;
  8 import java.io.FileWriter;
  9 import java.io.IOException;
 10 import java.io.InputStreamReader;
 11 import java.io.UnsupportedEncodingException;
 12 import java.util.ArrayList;
 13 import java.util.List;
 14 import java.util.Scanner;
 15 
 16 public class word6 {
 17 public static void main(String []args) throws IOException
 18 {
 19     System.out.println("最长单词链为:");
 20     List<Integer> list=new ArrayList<>();
 21     List<Integer> list1=new ArrayList<>();
 22     Scanner scan=new Scanner(System.in);
 23     File f = new File("input.txt");
 24      FileInputStream fip = new FileInputStream(f);
 25     InputStreamReader reader = new InputStreamReader(fip, "gbk");
 26     StringBuffer sb = new StringBuffer();
 27     while (reader.ready()) {
 28         list.add((int) ch((char) reader.read()));
 29 //        sb.append(ch((char) reader.read()));
 30     }
 31 //    System.out.println(sb.toString());
 32     Object[]a=list.toArray();
 33     for(int i=0;i<list.size();i++)
 34     {
 35         if((char)(int)a[i]=='@')
 36         {
 37             list1.add(i);
 38         }
 39     }
 40     reader.close();
 41     fip.close();
 42     Object[]b=list1.toArray();
 43     String []c=new String[9];
 44     c[0]=""; c[1]=""; c[2]=""; c[3]=""; c[4]=""; c[5]=""; c[6]=""; c[7]=""; c[8]="";
 45     int temp=0;
 46     for(int i=0;i<list.size();i++)
 47     {
 48         
 49 //        if(((char)(int)a[i]==(char)(int)a[(int)b[temp]-1])&&((char)(int)a[i-1]=='@'))
 50 //        {
 51 //            for(int k=(int)b[temp]+1;k<(int)b[temp+1];k++)
 52 //            {
 53 //                System.out.print((char)(int)a[k]);
 54 //            }
 55 //        }
 56         if((char)(int)a[i]!='@')
 57         {
 58             c[temp]+=(char)(int)a[i];
 59         }
 60         if((char)(int)a[i]=='@')
 61         {
 62             temp++;
 63         }
 64     }
 65     int temp1=0;
 66     String num="";
 67     for(int i=0;i<9;i++)
 68     {
 69         if(i==0)
 70         {
 71             System.out.print(c[0]);
 72             num=c[0];
 73         }
 74         for(int k=i+1;k<9;k++)
 75         {
 76             if(c[temp1].charAt(c[temp1].length()-1)==c[k].charAt(0))
 77             {
 78                 System.out.print("-"+c[k]);
 79                 num+="-"+c[k];
 80                 temp1=k;
 81                 break;
 82             }
 83         }
 84     }
 85 
 86     FileWriter fw = null;
 87     try {
 88         //创建字符输出流
 89         fw = new FileWriter("output.txt");
 90         fw.write("'"+num+"'
");
 91     } catch (IOException ioe) {
 92         ioe.printStackTrace();
 93     } finally {
 94         //使用finally块来关闭文件输出流
 95         if (fw != null) {
 96             fw.close();
 97         }
 98     }
 99 }
100 static char ch(char c)
101 {
102     if(!(c>=97&&c<=122))
103         c+=32;
104     return c;
105 }
106 }

运行结果:

原文地址:https://www.cnblogs.com/0518liu/p/11070295.html