【作业】飘-字单词统计
package piao; import java.io.FileReader; import java.util.Scanner; import java.util.ArrayList; import ci.Ci; public class piao2 { public static void main(String[] args) { ArrayList<Ci> list=new ArrayList<Ci>(); ArrayList<String>wlist=new ArrayList<String>(); try { FileReader frw=new FileReader("wu.txt"); Scanner inw =new Scanner(frw); while(inw.hasNext()) { wlist.add(inw.next()); } } catch(Exception e) { } int flag=-1; int flag2=0;//1 单词 2 字母 3 单词前 4 字母前 5 退出 int xianshi=-1,xianshi2=-1; String word=""; String line=""; try { Scanner input=new Scanner(System.in); FileReader fr=new FileReader("piao.txt"); Scanner in =new Scanner(fr); do { System.out.println("请选择:1 单词 2 字母 3 单词前n 4 字母前n 5 退出 6 短语"); flag2=input.nextInt(); if(flag2==5) { break; } if(flag2==3||flag2==4) { System.out.println("请输入数量:"); xianshi=input.nextInt(); } if(flag2==1||flag2==3)//单词 { in.useDelimiter("[ \",.?':;'!-]"); while(in.hasNext()) { word=in.next(); //System.out.println(word); if(ifWord(word)) { if(ifWu(wlist,word)) { Ci ci=new Ci(word); //查重 flag=-1; for(int i=0;i<list.size();i++) { if(equal(ci,list.get(i))) { flag=i; break; } else { flag=-1; } } if(flag>=0) { ci.setNumber(list.get(flag).getNumber()+1); list.set(flag, ci); } else { list.add(ci); } } } } } if(flag2==2||flag2==4) { while(in.hasNext()) { line=in.nextLine(); //System.out.println(word); for(int k=0;k<line.length();k++) { char c=line.charAt(k); if(((c>='a'&&c<='z')||(c>='A'&&c<='Z'))) { Ci ci=new Ci(String.valueOf(line.charAt(k))); //查重 flag=-1; for(int i=0;i<list.size();i++) { if(equal(ci,list.get(i))) { flag=i; break; } else { flag=-1; } } if(flag>=0) { ci.setNumber(list.get(flag).getNumber()+1); list.set(flag, ci); } else { list.add(ci); } } } } } if(flag2==6) { } while(in.hasNext()) { word=in.next(); //System.out.println(word); if(ifWord(word)) { Ci ci=new Ci(word); //查重 flag=-1; for(int i=0;i<list.size();i++) { if(equal(ci,list.get(i))) { flag=i; break; } else { flag=-1; } } if(flag>=0) { ci.setNumber(list.get(flag).getNumber()+1); list.set(flag, ci); } else { list.add(ci); } } } /*for(int i=0;i<list.size();i++) { list.get(i).show(); }*/ int t,k; for(t=0;t<list.size()-1;t++) { for(k=0;k<list.size()-t-1;k++) { if(!DX(list.get(k),list.get(k+1))) { Ci ci1=new Ci(list.get(k).getWord(),list.get(k).getNumber()); Ci ci2=new Ci(list.get(k+1).getWord(),list.get(k+1).getNumber()); list.set(k, ci2); list.set(k+1, ci1); } } } //显示 if(xianshi<0) { xianshi2=list.size(); } else { xianshi2=xianshi; } for(int i=0;i<xianshi2;i++) { list.get(i).show(); } }while(flag2!=5); } catch(Exception e) { } } public static Boolean DX(Ci a,Ci b) { if(a.getNumber()>=b.getNumber()) { return true; } else { return false; } } public static Boolean equal(Ci a,Ci b) { if(a.getWord().equals(b.getWord())) { return true; } else { return false; } } public static Boolean ifWu(ArrayList<String> wlist,String s)//无用词 { Boolean flag=true; for(int i=0;i<wlist.size();i++) { if(wlist.get(i).equals(s)) { flag=false; } } return flag; } public static Boolean ifWord(String s)//是否为单词 { Boolean flag=true; char c; if(s.equals(" ")||s.equals("")) { return false; } for(int i=0;i<s.length();i++) { c=s.charAt(i); if(!((c>='a'&&c<='z')||(c>='A'&&c<='Z'))) { flag=false; } } return flag; } public static Boolean ifZiMu(char c)//是否为字母 { Boolean flag=true; if(!((c>='a'&&c<='z')||(c>='A'&&c<='Z'))) { flag=false; } return true; } }