Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class WordCount {
- /**
- * Reads in a command-line integer and sequence of words from
- * standard input and prints out a word (whose length exceeds
- * the threshold) that occurs most frequently to standard output.
- * It also prints out the number of words whose length exceeds
- * the threshold and the number of distinct such words.
- */
- public static void main(String[] args) {
- In in = new In(args[0]);
- int distinct = 0, words = 0;
- int minlen = Integer.parseInt(args[0]);
- ST<String, Integer> st = new ST<String, Integer>();
- // compute frequency counts
- while (!in.isEmpty()) {
- String key = in.readString();
- if (key.length() < minlen) continue;
- words++;
- if (st.contains(key)) {
- st.put(key, st.get(key) + 1);
- }
- else {
- st.put(key, 1);
- }
- }
- // find a key with the highest frequency count
- String max = "";
- for (String word : st.keys()) {
- if (st.get(word) > st.get(max))
- max = word;
- }
- System.out.println(max + " " + st.get(max));
- System.out.println("distinct = " + distinct);
- System.out.println("words = " + words);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement