所用时间:一下午
代码:83
博客:3
知识点:
Android的案例计算器
课堂练习,寻找首尾相接链
1 package com.text; 2 3 import org.omg.CORBA.WStringSeqHelper; 4 5 import java.io.*; 6 import java.util.ArrayList; 7 import java.util.Objects; 8 9 public class file4 { 10 public static void main(String[] args) throws Exception { 11 File file = new File("src/input3919.txt"); 12 if (file.exists() && file.length() == 0) { 13 System.out.println(" 文件为空!"); 14 System.exit(0); 15 } else if (achieve() == 1) { 16 System.out.println(" 文件只有一個單詞!"); 17 System.exit(0); 18 } else { 19 achieve(); 20 } 21 } 22 23 public static long achieve() throws Exception { 24 // ArrayList<String> list = new ArrayList<String>(); 25 ArrayList<String> list2 = new ArrayList<String>();//存储遍历结果 26 27 File f1 = new File("src/text.txt"); 28 BufferedReader br = new BufferedReader(new FileReader(f1)); 29 //定义个字符串用来保存每次读取的数据 30 String len; 31 StringBuffer sb = new StringBuffer(); 32 while ((len = br.readLine()) != null) { 33 sb.append(len);//将文件内容存入sb中 34 } 35 //不区分大小写 36 String str = sb.toString().toLowerCase(); 37 String[] strings = str.split("[^a-zA-Z\\']+"); 38 39 String tail, head, word; 40 long total = 1, max = 0; 41 42 for (int i = 0; i < Objects.requireNonNull(strings).length; i++) { 43 ArrayList<String> list1 = new ArrayList<String>();//存储遍历结果 44 total = 1; 45 46 word = strings[i]; 47 list1.add(word); 48 tail = word.substring(word.length() - 1, word.length()); 49 50 // System.out.println(word); 51 52 for (int j = i + 1; j < Objects.requireNonNull(strings).length; j++) { 53 word = strings[j]; 54 head = word.substring(0, 1); 55 if (tail.equals(head)) { 56 list1.add(word); 57 //System.out.println(list1.get(i)); 58 tail = word.substring(word.length() - 1, word.length()); 59 total++; 60 } else { 61 //System.out.println(total); 62 if (total > max) { 63 64 list2 = list1; 65 max = total; 66 } 67 break; 68 } 69 } 70 } 71 System.out.println(max); 72 73 File file = new File("src/output.txt"); 74 FileWriter out = new FileWriter(file); 75 for (int i = 0; i < list2.size(); i++) { 76 System.out.println(list2.get(i)); 77 out.write(list2.get(i) + " "); 78 } 79 br.close(); 80 out.close(); 81 return max; 82 } 83 }
标签:总结,word,每日,System,println,new,ArrayList,out From: https://www.cnblogs.com/fan-wang/p/17162096.html