// <pre>
// --------------------------------------------------------------------------------
// file: Wfreq.java

import java.util.*;

/** Find frequencies of words in a given text.
 * @author Jaanus Poial
 * @version 0.3
 * @since 1.2
 */
public class Wfreq {

   /** Main method, the text is read from the command line. */
   public static void main (String[] arg) {
      
      // form a table of frequencies
      HashMap ftable = calcTable (arg);

      // sort keys alphabetically
      Object [] oa = ftable.keySet().toArray();
      Arrays.sort (oa);

      // output pairs "word - frequency"
      for (int i=0; i < oa.length; i++) {
         String word = (String)oa[i];
         int frequency = ((Integer)ftable.get (word)).intValue();
         System.out.println (word + " " + String.valueOf (frequency));
      }

   } // main

   /** Calculate frequencies.
    * @param t text as an array of strings
    * @return hashtable of "String-Integer"-pairs
    */
   public static HashMap calcTable (String[] t) {
      HashMap result = new HashMap();
      for (int i=0; i < t.length; i++) {
         StringTokenizer st = new StringTokenizer (t[i]);
         while (st.hasMoreTokens()) {
            String word = st.nextToken();
            if (result.containsKey (word)) {
               int k = ((Integer)result.get (word)).intValue();
               result.put (word, new Integer (k+1));
            } else {
               result.put (word, new Integer (1));
            }
         }
      }
      return result;
   } // calcTable

} // Wfreq

// end of file
// -----------------------------------------------------------------------------
// </pre>

