单词接龙

  今天的课堂测是单词接龙,

  例:apple  zoo  elephant   try  

最长的是1 ,3,4,

上源码

 1 public class Lan {
 2         @SuppressWarnings("unused")
 3             public static void danci(String s) throws IOException {
 4                    
 5                     BufferedReader br = new BufferedReader(new FileReader(s));
 6                     StringBuffer sb = new StringBuffer();
 7                     String text = null;
 8                     while ((text = br.readLine()) != null) {
 9                         sb.append(text);// 将读取出的字符追加到stringbuffer中
10                     }
11                     br.close(); // 关闭读入流
12                     String str = sb.toString().toLowerCase(); // 将stringBuffer转为字符并转换为小写
13                     String[] words = str.split("[^(a-zA-Z)]+"); // 非单词的字符来分割,得到所有单词
14                     StringBuffer yao = new StringBuffer();
15                     String b1=words[0];
16                     yao.append(b1);
17                     yao.append(" ");
18                     
19                     String end=b1.substring(b1.length()-1,b1.length());
20                     
21                    for(int i=1;i<words.length;i++)
22                    {  
23                     String start=words[i].substring(0,1);
24                     if(end.equals(start))
25                     {
26                         end=words[i].substring(words[i].length()-1,words[i].length());
27                         yao.append(words[i]);
28                         yao.append("  ");
29                     }
30                    }
31                    
32                 
33                    File file =new File("output.txt");
34                     try {
35                          file.createNewFile();
36                     } catch (IOException e) {
37                        e.printStackTrace();      
38                    }
39                   
40                     try {
41                         
42                           FileWriter fw =new FileWriter(file);
43                           fw.write(yao.toString());
44                           fw.flush();
45                           fw.close();
46                     }
47                     catch (IOException e) {
48                            e.printStackTrace();      
49                        }
50                  
51             }
52 
53 
54         // 判断文件是否存在
55         public static boolean judeFileExists(File file) {
56 
57             if (file.exists()) {
58                 System.out.println("输入文件存在");
59                 return true;
60             } else {
61                 System.out.println("输入文件不存在");
62 
63                 return false;
64             }
65         }
66         public static void main(String[] args) throws IOException {
67                 // TODO 自动生成的方法存根
68                 String filename = "input.txt";
69                 File a = new File(filename);
70                 if (judeFileExists(a)) {
71                     danci(filename);
72                 } else {
73                 }
74             }
75         }

 这是效果图,

还有文件提示。

原文地址:https://www.cnblogs.com/msdog/p/10988002.html