I, entitled
Second, the source code
import java.io.*; import java.util.*; public class ReadWord2 { private static String str1; private static String [] Str=new String[20000]; private static int [] n=new int [20000]; public static void main(String [] args) throws IOException { int num=0,i=0,j=0; double sum=0; Scanner input =new Scanner(System.in); File file =new File("src/Harry.txt"); String get=null; BufferedReader reader =new BufferedReader(new FileReader(file)); while((get=reader.readLine())!=null) { StringTokenizer sc =new StringTokenizer(get,",.?''\" -"); while(sc.hasMoreElements()) { str1=(String)sc.nextElement(); str1=str1.toLowerCase(); for(i=0;i<=num;i++) { if(str1.equals(Str[i])) { n[i]++; break; } } if(i>num) { Str[num]=str1; n[num]=1; num++; } } } for(i=0;i<num;i++) { for(j=i;j<num;j++) { if(n[i]<n[j]) { int temp=n[i]; n[i]=n[j]; n[j]=temp; String tp=Str[i]; Str[i]=Str[j]; Str[j]=tp; } } } i=0; while(n[i]!=0) { sum+=n[i]; i++; } System.out.println("单词总数为"+SUM); System.out.println ( "Enter the number of words before the number you want to check a few" ); int N = input.nextInt (); for (i = 0; i <N; i ++ ) { System. Out.print ( "first" + (i + 1) + " multi-word is" + Str [i] + "total" + n [i] + "a" ); of System.out.print ( "the total word a " ); System.out.printf ( "% .2f ", n-[I] * 100 / SUM); System.out.println ( "% " ); } } }
Third, experience
Some ideas just started really did not, because of the need from the file to every word points out, this is the place where I had a headache. Then, after drawing people's code I found a great way StingTokenizer, a string can be divided according to the custom symbol into a string, and then stored. BufferedReader is then also learned a buffered stream, although not required, but can speed read.