import structure.*; public class WordFreq { //+implementation public static void main(String args[]) { Vector vocab = new Vector(1000); ReadStream r = new ReadStream(); int i; // for each word on input for (r.skipWhite(); !r.eof(); r.skipWhite()) { Association wordInfo; // word-frequency association String vocabWord; // word in the list // read in and tally instance of a word String word = r.readString(); for (i = 0; i < vocab.size(); i++) { // get the association wordInfo = (Association)vocab.elementAt(i); // get the word from the association vocabWord = (String)wordInfo.key(); if (vocabWord.equals(word)) { // match: increment integer in association Integer f = (Integer)wordInfo.value(); wordInfo.setValue(new Integer(f.intValue() + 1)); break; } } // mismatch: add new word, frequency 1. if (i == vocab.size()) { vocab.addElement( new Association(word,new Integer(1))); } } // print out the accumulated word frequencies for (i = 0; i < vocab.size(); i++) { Association wordInfo = (Association)vocab.elementAt(i); System.out.println( wordInfo.key()+" occurs "+ wordInfo.value()+" times."); } } //-implementation }