单词统计



	import java.util.*;
	import java.io.*;
	import java.util.Scanner;
	public class test1 
	{
	    static int ha=0;
	    static Scanner sc=new Scanner(System.in);
	    public static void main(String[] args)throws IOException
	    {
	        Map<String,Integer> map=new HashMap<String, Integer>();//通过map保存映射,和数组类似
	        File file=new File("E:\ALL Download\466831.txt");
	        FileReader fr=new FileReader(file);
	        try
	        {
	            BufferedReader bd=new BufferedReader(fr);
	            String wen=null;
	            while((wen=bd.readLine())!=null)//读入一行数据
	            {
	                String []word=wen.split(" ");//通过空格将整行数据分成多个字符串并保存在字符串数组里
	                ha+=word.length;
	                for(int i=0;i<word.length;i++)
	                {
	                    if(word[i].equals(" "))
	                    {
	                        continue;
	                    }
	                    if(map.containsKey(word[i]))//检查集合中是否有这个元素
	                    {
	                         Integer a=map.get(word[i]);
	                         a++;
	                         map.put(word[i], a);//为他出现的次数加一
	                    }
	                    else
	                        map.put(word[i],1);//如果从未出现过就将他的values赋值为一
	                    
	                }
	                map.put("   ",0);
	            }
	            fr.close();
	        }catch (Exception e)//程序的异常处理
	        {
	            e.printStackTrace();
	        }
	        File file2=new File("E:\ALL Download\11466831.txt");
	        if(!file2.exists())
	        {
	            try
	            {
	                file2.createNewFile();
	                System.out.println("数据输出的指向文件不存在已经为您新建一个以保留运行结果请继续操作");
	            }
	            catch(Exception e)
	            {
	                e.printStackTrace();
	            }
	        }
	        FileWriter fw=new FileWriter(file2);
	        BufferedWriter bw=new BufferedWriter(fw);
	        System.out.println("输入个数");
	        Integer shu=sc.nextInt();
	        Integer max1;
	        String max2="";
	        for(int b=1;b<=shu;b++)
	        {
	            max1=0;
	            max2=null;
	            //找出出现次数最多的单词
	            Set<String> set=map.keySet();//构建map集合所有key对象集合
	            Iterator <String> it=set.iterator();//创建集合迭代器
	            while(it.hasNext())
	            {
	                String key1=it.next();
	                Integer a2=map.get(key1);
	                if(a2>max1)
	                {
	                    max1=a2;
	                    max2=key1;
	                }
	            }
	            //主要是为了判断是否会出现出现次数相同的单词
	            Set<String> set2=map.keySet();//构建map集合所有key对象集合
	            Iterator <String> it2=set2.iterator();//创建集合迭代器
	            while(it2.hasNext())
	            {
	                String key2=it2.next();
	                Integer a3=map.get(key2);
	                if(a3==max1)//判断是否有出现次数相同的字母,如果有的话全部输出
	                {
	                    double ans=max1*1.0/ha;
	                    
	                    bw.write("出现次数排在第  "+b+" 位的单词是  "+key2+" 出现次数是 "+String.format("%.2f", ans*100)+"%");
	                    bw.newLine();
	                    System.out.println("出现次数排在第  "+b+" 位的单词是  "+key2+" 出现次数是  "+String.format("%.2f", ans*100)+"%");
	                    map.put(key2,0);//输出之后让他的values变为0,防止阻碍后面的判断
	                }
	            }
	            
	        }
	        System.out.println("相关数据已经全部写入相应的文件夹里(在屏幕上也进行了显示)");
	        bw.close();
	        fw.close();
	    }
	}



原文地址:https://www.cnblogs.com/deepend/p/13084136.html