首页 > 其他 > 详细

课堂作业

时间:2018-10-12 01:13:36      阅读:32      评论:0      收藏:0      [点我收藏+]

标签:for   exc   思路   stack   imp   get   nbsp   单词   出现的次数   


package chachong;
import chachong.WordEntity;

public class WordEntity implements Comparable<WordEntity>{

    public int compareTo(WordEntity o) {
        int cmp = count.intValue() - o.count.intValue();
        return (cmp == 0 ? key.compareTo(o.key) : -cmp);
    }
 
    private String key;
    private Integer count;
 
    public WordEntity ( String key,Integer count) {
        this.key = key;
        this.count = count;
    }
 
    public WordEntity(){
 
    }
 
    public String toString() {
        return key + " 出现的次数为:" + count;
    }
 
    public String getKey() {
        return key;
    }

    public int getCount() {
        return 0;
    }
}

 

Wordcont.java
package
chachong; import java.io.BufferedReader; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import java.util.HashMap; import java.util.Iterator; import java.util.Map; import java.util.Set; import java.util.StringTokenizer; import java.util.TreeMap; import java.util.TreeSet; import chachong.WordEntity; public class WordCont { public void displayWordCount(String fileName) { try {@SuppressWarnings("resource") BufferedReader reader = new BufferedReader(new FileReader(fileName)); String line = null; TreeMap<String,Integer> tm = new TreeMap<String,Integer>(); while((line=reader.readLine())!=null) { line = line.toLowerCase(); String str[] = line.split("\\s+"); for(int i = 0; i<str.length; i++) { String word = str[i].trim(); if(tm.containsKey(word)) { tm.put(word, tm.get(word)+1); }else { tm.put(word, 1); } } } System.out.println("按字序输出为:"); Iterator<?> iterator=tm.entrySet().iterator(); while(iterator.hasNext()) System.out.println(iterator.next()); }
package chachong;
import java.util.Scanner;

import chachong.WordCont;


public class ChaChong {
    public static void main(String[] args) {
        System.out.println("输入文件路径:\n");            
        @SuppressWarnings("resource")
        Scanner in=new Scanner(System.in);
        String line=in.nextLine();
        String fileName= line.trim();
        WordCont wc = new WordCont();
        wc.displayWordCount(fileName);
        wc.displayFrequencyWord(fileName);
    }
 
}



            e.printStackTrace();
           }
     

}
          public void displayFrequencyWord(String fileName){
                //显示输出
                try {
                    @SuppressWarnings("resource")
                    BufferedReader br = new BufferedReader(new FileReader(fileName));
                    String s;
                    StringBuffer sb = new StringBuffer();
                    while ((s = br.readLine()) != null) {
                        sb.append(s);
                    }
                    
                    Map<String,Integer> map = new HashMap<String, Integer>();
                    StringTokenizer st = new StringTokenizer(sb.toString(),",.! \n");
                    while (st.hasMoreTokens()) {
                        String letter = st.nextToken().trim();
                        int count;
                        if (!map.containsKey(letter)) {
                            count = 1;
                        } else {
                            count = map.get(letter).intValue() + 1;
                        }
                        map.put(letter,count);
                    }
                    
                    Set<WordEntity> set = new TreeSet<WordEntity>();
                    for (String key : map.keySet()) {
                        set.add(new WordEntity(key,map.get(key)));
                    }
         
                    System.out.println("出现频率最高的单词:");
                    Iterator<WordEntity> it1 = set.iterator();
                    int count=it1.next().getCount();
                    for (Iterator<WordEntity> it = set.iterator(); it.hasNext(); ) {
                        WordEntity w = it.next();
                        
                        if (w.getCount()==count)// 当输出3个后跳出循环
                            //break;
                        
                        System.out.println(w.getKey() + " 出现的次数为: "+ w.getCount());
                        
                    }
                } catch (FileNotFoundException e) {
                    System.out.println("文件未找到~!");
                } catch (IOException e) {
                    System.out.println("文件读异常~!");
                }
         
            }
         
        }

    

 本次课堂作业一开始思路不是很清晰,所以在网上找了一些资料,根据网上一些程序外加自己的一些思路来编译出来的,但是还是有一些小错误,感觉自己还有很多的不足,需要更加认真的学习

课堂作业

标签:for   exc   思路   stack   imp   get   nbsp   单词   出现的次数   

原文:https://www.cnblogs.com/love-nan/p/9775855.html

(0)
(0)
   
举报
评论 一句话评论(0
0条  
登录后才能评论!
© 2014 bubuko.com 版权所有 鲁ICP备09046678号-4
打开技术之扣,分享程序人生!
             

鲁公网安备 37021202000002号