查询一本书籍中出现次数最多的单词和他的次数

《飘》

今天我们做一个查询书籍中出现次数最多的单词和他的次数。

思路:

首先先进行文件的读入,我用的是BufferedReader来进行读入,我们需要考虑的是如何将读入的单词划分成单独的个体,还要对其进行计数,我们很自然地想到了用Map方法来进行计数,一个key值对应着一个value。

①读入文档的第一行

②将读入的这一行的单词用split进行分割,返回对应的单词数组

③对每一个单词数组中的单词进行判断是否出现在Map集合里,若出现,则取出他的value,进行+1操作,然后再放回去;否则,直接将其key值和value=1放进Map集合里。

④文档都被读完后,开始进行比较,找出出现次数最多的单词和他的次数。

⑤在创建一个文件,来写入该数据到文件中

代码:

package com.wenjian;
import java.io.*;
import java.util.*;

public class piao {

    public static void main(String[] args) throws IOException{
        // TODO Auto-generated method stub
        Map<String, Integer> map = new HashMap<>();
        //打开文件
        File file=new File("F:\\JAVA程序集合\\01JAVA\\src\\com\\wenjian\\飘英文版.txt");
        FileReader fr = new FileReader(file);
        try {
            BufferedReader buf1=new BufferedReader(fr);
            String s=null;
            while((s=buf1.readLine())!=null)//读入文档一行,直到没有单词为止
            {
                String[] words=s.split(" ");//将该行的字符串分割成单词数组
                for(int i=0;i<words.length;i++)
                {
                    if(map.containsKey(words[i]))//判断每一个单词数组是否在Map集合里
                    {
                        Integer x=map.get(words[i]);//取出对应的value值
                        x++;//进行+1操作
                        map.put(words[i], x);//再将该一对值放进集合,集合只会记录最后一次放进的数据
                    }
                    else
                        map.put(words[i],1);//将新的单词放进集合
                }
            }
            fr.close();
            } catch (FileNotFoundException e1) {
                e1.printStackTrace();
                }
        //开始查找出现次数最多的单词
        String ans="";
        int maxn=-5;
        Iterator<String> iter=map.keySet().iterator();
        while(iter.hasNext())
        {
            String key=iter.next();
            int m=map.get(key);
            if(m>maxn)
            {
                maxn=m;
                ans=key;
            }
        }
        System.out.println("最多出现的单词是:"+ans+"\n出现的次数是:"+map.get(ans));
        //将出现次数最多的单词和对应的次数写进文件里
        File file1=new File("1.txt");
        if(!file.exists())
        {
            System.out.println("该文件不存在");
            return ;
        }
        else
        {
            try {
                FileWriter fw=new FileWriter(file1);
                BufferedWriter bufw=new BufferedWriter(fw);
                bufw.write("最多出现的单词是:"+ans+"\n出现的次数是:"+map.get(ans));
                bufw.close();
                fw.close();
            }catch (Exception e) {
                // TODO: handle exception
                e.printStackTrace();
            }
        }
    }
}

 

 

 

 

还有一些其他写法:

package com.wenjian;
import java.io.*;
import java.util.*;
public class test1 {

    public static void main(String[] args) throws FileNotFoundException {
        // TODO Auto-generated method stub
         File file=new File("F:\\JAVA程序集合\\01JAVA\\src\\com\\wenjian\\飘英文版.txt");                  //读取文件
            if(!file.exists()){
                System.out.println("文件不存在");
                return;
            }  
        Scanner scanner=new Scanner(file);
        
        Map<String, Integer> map = new HashMap<>();
        while(scanner.hasNextLine())
        {
            String line=scanner.nextLine();
             String[] lineWords=line.split(" "); //利用split将整行字符串分割成单个字符串数组
             for(int i=0;i<lineWords.length;i++) {
                 if(map.containsKey(lineWords[i]))
                 {
                     Integer x=map.get(lineWords[i]);
                     x++;
                     map.put(lineWords[i],x);
                 }
                 else
                     map.put(lineWords[i],1);
             }
        }
        String ans="";
        int maxn=-5;
        Iterator<String> iter=map.keySet().iterator();
        while(iter.hasNext())
        {
            String key=iter.next();
            int m=map.get(key);
            if(m>maxn)
            {
                maxn=m;
                ans=key;
            }
        }
        System.out.println(ans+"出现了"+map.get(ans));
    }

}
posted @ 2019-09-25 19:08  清风紫雪  阅读(749)  评论(0编辑  收藏  举报