• 【个人作业】单词链


    import java.util.ArrayList;
    import java.util.List;
    
    public class Chain {
        
        private static int chaincount = 0;
        private static char startal;
        private static char cuendal;
        private static List<ChainBean> chainlist = new ArrayList<ChainBean>();
        private static FileIOStream fileios = new FileIOStream();
        private static List<String> strlist =null;
        
        public static void main(String [] args)
        {
            /*
             * 读取文件到strlist
             * 遍历strlist,以非单词字符为间隔,存储到string变量word里,word首字母startal
             * 遍历ChainList,若有endal=startal,则添加到其wordlist。若无,则add一个ChainList
             * 找出最长的ChainList,储存到文件
             */
            
            
            if(fileios.readFile())
            {
                strlist = fileios.getStrlist();
                System.out.println("file read end");
                for(int i = 0 ; i < strlist.size() ; i++) {
                    String s = strlist.get(i);
                    extractWord(s);
                }
                selectLongChain();
            }
            
        }
        
        private static  void extractWord(String s)
        {
            System.out.println("extract word start");
            int t = 0;
            String word = "";
            for(int j=0; j<s.length(); j++)
            {
                char c = s.charAt(j);
                boolean al = (((c>=65)&&(c<=90))||((c>=97)&&(c<=122)));
                System.out.println("c = "+c+" , al = "+al);
                if((t==0)&&al)
                {
                    startal = c;
                    System.out.println("the new word startal = "+startal);
                    t = 1;
                }
                if((t==1)&&al)
                {
                    cuendal = c;
                    word += c;
                }
                if((t==1)&&!al)
                {
                    System.out.println("extract word : "+word);
                    judgeStartAl(word);
                }
            }
            System.out.println("extract word :"+word);
            judgeStartAl(word);
            System.out.println("extract word end");
        }
        
        private static void judgeStartAl(String word)
        {
            System.out.println("judge startal start");
            int t=0;
            for(int i=0;(chaincount != 0)&&( i < chainlist.size())&&(t==0);i++)
            {
                System.out.println("current endal = "+chainlist.get(i).getEndal()+" , and the statral = "+startal);
                if((chainlist.get(i).getEndal() == startal)||chainlist.get(i).getEndal() == (startal + 32))
                {
                    System.out.println("insert word "+word);
                    chainlist.get(i).addWordlist(word);
                    chainlist.get(i).setEndal(cuendal);
                    t = 1;
                }
            }
            if(t==0)
            {
                System.out.println("add word "+word+" in a new chain");
                System.out.println("word = "+word);
                List<String> wordlist = new ArrayList<String>();
                wordlist.add(word);
                ChainBean chainbean = new ChainBean(wordlist,cuendal);
                System.out.println("the new chain endal = "+cuendal);
                chainlist.add(chainbean);
                chaincount++;
            }
        }
        
        private static void selectLongChain()
        {
            int max = 0;
            int t = 0;
            for(int i=0; i<chainlist.size();i++)
            {
                int length = chainlist.get(i).getWordlist().size();
                if(max < length)
                {
                    max = length;
                    t = i;
                }
            }
            if(chaincount != 0)
            {
                fileios.writeFile(chainlist.get(t).getWordlist());
            }
            else
            {
                System.out.println("the file is null");
            }
        }
    
    }
    import java.util.List;
    
    public class ChainBean {
        
        private List<String> wordlist;
        private char endal;
        
        public ChainBean(List<String> wordlist,char endal)
        {
            this.wordlist = wordlist;
            this.endal = endal;
        }
        
        public List<String> getWordlist() {
            return wordlist;
        }
        public void addWordlist(String word) {
            wordlist.add(word);
        }
        public char getEndal() {
            return endal;
        }
        public void setEndal(char endal) {
            this.endal = endal;
        }
        
        
        
    
    }
    import java.io.BufferedReader;
    import java.io.BufferedWriter;
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.io.FileReader;
    import java.io.FileWriter;
    import java.io.IOException;
    import java.util.ArrayList;
    import java.util.List;
    
    public class FileIOStream {
    
        String filename = "E:\Homework\大二下\软工\input1.txt";
        String filename2 = "E:\Homework\大二下\软工\output1.txt";
        List<String> strlist = new ArrayList<String>();
        
        public void setStrlist(List<String> strlist)
        {
            this.strlist = strlist;
        }
        
        public List<String> getStrlist()
        {
            return strlist;
        }
        
        public boolean readFile()
        {
            boolean fileexists = true;;
            File file = new File(filename);
            if(!file.exists())
            {
                System.out.println("文件不存在");
                fileexists = false;
                return fileexists;
            }
            String s = null;
            BufferedReader br = null;
            try
            {
                br = new BufferedReader(new FileReader(filename));
                while((s = br.readLine()) != null) {  
                    strlist.add(s);
                }
                System.out.println("文件读取成功");
            }catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } finally
            {
                try
                {
                    br.close();
                }catch(Exception e)
                {
                    e.printStackTrace();
                }
            }
            return fileexists;
        }
        
        public void writeFile(List<String> wordlist)
        {
            File file = new File(filename2);
            try {
                file.createNewFile();
                try (FileWriter writer = new FileWriter(file);
                         BufferedWriter out = new BufferedWriter(writer)
                    ) {
                        for(int j = 0 ; j < wordlist.size() ; j++)
                        {
                            String s = wordlist.get(j);
                            out.write(s+"
    ");
                        }
                        out.flush(); // 把缓存区内容压入文件
                        System.out.println("文件写入成功");
                    }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        
    }
  • 相关阅读:
    Linux imooc learning
    有用的生活有关的website
    ps -ef | grep java
    2.3. Configuring sudo Access-RedHat
    How to change java version in Linux
    文档记录工具
    Jmeter 学习imooc
    Linux 用户管理
    Builder模式(设计模式)
    Prototype模式(设计模式)
  • 原文地址:https://www.cnblogs.com/gothic-death/p/11071664.html
Copyright © 2020-2023  润新知