今天在软件工程课程上主要进行了学习方法,与老师的教学方法与目标的分享,告诉我们要在“做中学”,还给我们留了作业的目标,这学期各个阶段
应该的学习的内容(包括每天一条学习进度表格)。最后进行了课堂的练习,主要是
计算最长英语单词链
一个文本文件中有N 个不同的英语单词, 我们能否写一个程序,快速找出最长的能首尾相连的英语单词链,每个单词最多只能用一次。
import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.io.Writer; import java.text.DecimalFormat; import java.text.NumberFormat; import java.util.ArrayList; import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Scanner; public class WordConnect { static int N=5; static String b[]=new String[500]; public static void main(String[] args) { String sz=writeFromFile.readTxtFile("input3.txt"); String ltxt=null; if(sz==null) { System.out.println("找不到指定的文件"); } else if(sz=="kong") { System.out.println("文件为空!"); } else { System.out.println(ltxt=StatList1(sz)); select(b); } try { writeFromFile.daochu(ltxt); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public static int woor(String a) { int n=0; File ctoFile = new File("stopword.txt"); InputStreamReader rdCto; try { rdCto = new InputStreamReader(new FileInputStream(ctoFile)); BufferedReader bfReader = new BufferedReader(rdCto); String txtline = null; try { while ((txtline = bfReader.readLine()) != null) { if(txtline.equals(a)) { n=1; } } bfReader.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } return n; } public static ArrayList<String> getFiles(String path) { ArrayList<String> files = new ArrayList<String>(); File file = new File(path); File[] tempList = file.listFiles(); for (int i = 0; i < tempList.length; i++) { if (tempList[i].isFile()) { files.add(tempList[i].toString()); } if (tempList[i].isDirectory()) { } } return files; } public static String StatList1(String str) { StringBuffer sb = new StringBuffer(); HashMap<String ,Integer> has = new HashMap<String ,Integer> (); String[] slist = str.split("[^a-zA-Z\']+"); for (int i = 0; i < slist.length; i++) { if (!has.containsKey(slist[i])) { has.put(slist[i], 1); } else { has.put(slist[i],has.get(slist[i])+1 ); } } Iterator<String> iterator = has.keySet().iterator(); String a[]=new String[500]; int s[]=new int[500]; int judge; int n=400; for(int i=0;i<n;i++) { iterator = has.keySet().iterator(); while(iterator.hasNext()) { String word = (String) iterator.next(); if(s[i]<has.get(word)) { s[i]=has.get(word); a[i]=word; b[i]=word; } } judge=woor(a[i]); if(judge==1) { n++; has.remove(a[i]); } else { has.remove(a[i]); } } return sb.toString(); } public static void select(String b[]) { List<Object> list=new ArrayList<>(); int q=0; int n0=0; for(int i=0;i<b.length;i++) { if(b[i]!=null) { n0++; } } if(n0==1) { System.out.println("只有一个单词"); } else { String c="and"; q=c.length(); char n1=c.charAt(0); char n2=c.charAt(q-1); list.add(c); for(int j=0;j<b.length;j++) { String w=b[j]; if(w!=null) { int e=w.length(); char n3=w.charAt(0); char n4=w.charAt(e-1); if(n2==n3) { list.add(w); n1=n3; n2=n4; } } } String l=null; if(list.size()==1) { System.out.println("没有首尾相连"); } else { for(int i=0;i<list.size();i++) { l=l+list.get(i)+"->"; System.out.println(list.get(i)+"->"); } try { daochu(l); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } } public static void daochu(String a) throws IOException { File file =new File("output1.txt"); Writer out =new FileWriter(file); String data=a; out.write(data); out.close(); }
所花时间(包括上课) |
5h |
代码量(行) |
110行 |
博客量(篇) |
1篇 |
了解到的知识点 |
提取文本(txt)以及将文本输出,“做中学”,Android studio 虚拟机的配置 |
标签:java,String,2024.03,05,int,io,new,import From: https://www.cnblogs.com/dmx-03/p/18054778