首页 > 其他 > 详细

检验重复字母代码

时间:2018-10-12 01:12:31      阅读:219      评论:0      收藏:0      [点我收藏+]
//信1705-2   20173629   何伟豪
package 论文查重;
import java.io.*;
import java.util.*;

public class 查重 {
    public static void main(String[] args){
        demo(new File("C:\\Users\\Lenovo\\Desktop\\JAVA\\临时文件\\论文查重\\论文查重\\作文.txt"));
    }
    public static void demo(File file){
        BufferedReader bfr = null;   //定义字符读取(缓冲)流
        try{
            bfr = new BufferedReader(new FileReader(file));
            String value = null; 
            String newValue = "";    
            while((value = bfr.readLine())!=null){    //开始读取文件中的字符
                newValue = newValue+value;    //存入newValue变量中
            }
            char[] ch = newValue.toCharArray();//把newValue变成字符数组
            TreeMap<Character,Integer> tm = new TreeMap<Character,Integer>(Collections.reverseOrder());
            for(int x = 0;x<ch.length;x++){   
                char c = ch[x];
                if(tm.containsKey(c)){  //如果TreeMap(tm)中有该键,则取出该键中的值,也就是出现的次数
                    int conut = tm.get(c);
                    tm.put(c,conut+1);  //存入把新值存入tm集合中,如果键相同的话, 新键会替换老键,值也随着变化了
                }
                else{
                    tm.put(c, 1);  //如果没有出现该键就说明是第一次出现,然后就存入1次
                }
            }
            //下面的是取出TreeMap(tm)中的键和值
            Set<Map.Entry<Character, Integer>> set = tm.entrySet();
            Iterator<Map.Entry<Character, Integer>> iter = set.iterator();
            while(iter.hasNext()){
                Map.Entry<Character, Integer> map = iter.next();
                char k = map.getKey();
                int v = map.getValue();
                System.out.print(k+"("+v+")  ");
            }
        }
        catch(IOException e){
            System.out.println("文件读取错误");
        }
        finally{
            try{
                if(bfr!=null)
                    bfr.close();
            }
            catch(IOException e){
                System.out.println("文件关闭错误");
            }
        }
    }
}

 

检验重复字母代码

原文:https://www.cnblogs.com/hwh000/p/9775644.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!