问题描述
importjava.io.*;importjava.awt.*;importjava.io.File;importjava.awt.event.*;importjava.util.Vector;classWordStatistic{VectorallWords,noSameWord;WordStatistic(){allWords=newVector();noSameWord=newVector();}publicvoidwordStatistic(Filefile){try{RandomAccessFileinOne=newRandomAccessFile(file,"rw");RandomAccessFileinTwo=newRandomAccessFile(file,"rw");longwordStarPostion=0,wordEndPostion;longlength=inOne.length();intflag=1;intc=-1;for(intk=0;k<=length;k++){c=inOne.read();booleanboo=(c<='Z'&&c>='A')||(c<='z'&&c>='a');if(boo){if(flag==1){wordStarPostion=inOne.getFilePointer()-1;flag=0;}}else{if(flag==0){if(c==-1)wordEndPostion=inOne.getFilePointer();elsewordEndPostion=inOne.getFilePointer()-1;inTwo.seek(wordStarPostion);bytecc[]=newbyte[(int)wordEndPostion-(int)wordStarPostion];inTwo.readFully(cc);Stringword=newString(cc);allWords.add(word);if(!(noSameWord.contains(word)))noSameWord.add(word);}flag=1;}}inOne.close();inTwo.close();}catch(Exceptione){}}publicVectorgetAllWords(){returnallWords;}publicVectorgetNoSameWord(){returnnoSameWord;}}classStatisticFrameextendsFrameimplementsActionListener{WordStatisticstatistic;TextAreashowMessage;ButtonopenFile;FileDialogopenFileDialog;VectorallWord,noSameWord;publicStatisticFrame(){statistic=newWordStatistic();showMessage=newTextArea();openFile=newButton("OpenFile");openFile.addActionListener(this);add(openFile,BorderLayout.NORTH);add(showMessage,BorderLayout.CENTER);openFileDialog=newFileDialog(this,"打开文件对话框",FileDialog.LOAD);allWord=newVector();noSameWord=newVector();setSize(350,300);setVisible(true);addWindowListener(newWindowAdapter(){publicvoidwindowClosing(WindowEvente){System.exit(0);}});validate();}publicvoidactionPerformed(ActionEvente){noSameWord.clear();allWord.clear();showMessage.setText(null);openFileDialog.setVisible(true);StringfileName=openFileDialog.getFile();if(fileName!=null){statistic.wordStatistic(newFile(fileName));allWord=statistic.getAllWords();noSameWord=statistic.getNoSameWord();showMessage.append("n"+fileName+"中有"+allWord.size()+"个英文单词");showMessage.append("n其中有"+noSameWord.size()+"个互不相同英文单词");showMessage.append("n按使用率排列:n");intcount[]=newint[noSameWord.size()];for(inti=0;i<noSameWord.size();i++){Strings1=(String)noSameWord.elementAt(i);for(intj=0;j<allWord.size();j++){Strings2=(String)allWord.elementAt(j);if(s1.equals(s2))count[i]++;}}for(intm=0;m<noSameWord.size();m++){for(intn=m+1;n<noSameWord.size();n++){if(count[n]>count[m]){Stringtemp=(String)noSameWord.elementAt(m);noSameWord.setElementAt((String)noSameWord.elementAt(n),m);noSameWord.setElementAt(temp,n);intt=count[m];count[m]=count[n];count[n]=t;}}}for(intm=0;m<noSameWord.size();m++){showMessage.append("n"+(String)noSameWord.elementAt(m)+":"+count[m]+"/"+allWord.size()+"="+(1.0*count[m])/allWord.size());}}}}publicclassStatisticMainClass{publicstaticvoidmain(Stringargs[]){newStatisticFrame();}}
解决方案
解决方案二:
那位帮我看看,我想把输出的单词按字典序排列,由于刚接触Java,对有些知识还是不了解,谢谢了
解决方案三:
classWordStatistic有问题