Advertisement
Guest User

Untitled

a guest
Mar 3rd, 2015
194
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.32 KB | None | 0 0
  1. public class WordCount {
  2.     /**
  3.      * Reads in a command-line integer and sequence of words from
  4.      * standard input and prints out a word (whose length exceeds
  5.      * the threshold) that occurs most frequently to standard output.
  6.      * It also prints out the number of words whose length exceeds
  7.      * the threshold and the number of distinct such words.
  8.      */
  9.     public static void main(String[] args) {
  10.         In in = new In(args[0]);
  11.         int distinct = 0, words = 0;
  12.         int minlen = Integer.parseInt(args[0]);
  13.         ST<String, Integer> st = new ST<String, Integer>();
  14.  
  15.         // compute frequency counts
  16.         while (!in.isEmpty()) {
  17.             String key = in.readString();
  18.             if (key.length() < minlen) continue;
  19.             words++;
  20.             if (st.contains(key)) {
  21.                 st.put(key, st.get(key) + 1);
  22.             }
  23.             else {
  24.                 st.put(key, 1);
  25.             }
  26.         }
  27.  
  28.         // find a key with the highest frequency count
  29.         String max = "";
  30.         for (String word : st.keys()) {
  31.             if (st.get(word) > st.get(max))
  32.                 max = word;
  33.         }
  34.  
  35.         System.out.println(max + " " + st.get(max));
  36.         System.out.println("distinct = " + distinct);
  37.         System.out.println("words    = " + words);
  38.     }
  39. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement