统计小说《飘》中英文单词

package class20190923;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.*;
import java.util.StringTokenizer;
public class Classtext2 {
    private static String str1;
    private static String[] str3 = new String[20000];
    private static int[] n=new int[20000];
    
    public static void main(String[] args) throws IOException {
        
        Scanner sc=new Scanner(System.in);
        File file = new File("src/飘英文版.txt");
        String tempstr=null;
        BufferedReader reader = null;
        reader = new BufferedReader(new FileReader(file));
        int i=0,sum=0,j=0;
        while((tempstr=reader.readLine())!=null) {
        StringTokenizer st = new StringTokenizer(tempstr," .,?”“;:''   !—‘");
        while(st.hasMoreElements()) {
        str1=(String)st.nextElement();
        str1=str1.toLowerCase();
        for(i=0;i<=sum;i++)
        {
            if(str1.equals(str3[i])) {
                n[i]++;
                break;}
        }
        if(i>sum) {
            str3[sum]=str1;
            n[sum]=1;
            sum++;
            }
                }
                    }
        
        for( i=0;i<sum;i++) {
            for( j=i+1;j<sum;j++) {
                if(n[j]>n[i]) {
                    int temp=n[i];
                    n[i]=n[j];
                    n[j]=temp;
                    String Temp=str3[i];
                    str3[i]=str3[j];
                    str3[j]=Temp;
                }
            }
        }

        System.out.print("你要前几个最多的单词:");
         int choose = sc.nextInt();
         for(i=0;i<choose;i++) {
             System.out.println(str3[i]+"    "+n[i]);
         }
    }
}

结果:

你要前几个最多的单词:5
the
and
to
of
her

上一篇:python3 一个逗号引发的bug


下一篇:String字符串常用方法