import structure5.*; import java.util.Scanner; /* * After you compile this, use it like: * $ java WordFreq < words.txt * or * $ echo "here are some words words words" | java WordFreq */ class WordFreq { public static void main(String[] args) { // make a table Vector> words = new Vector>(); // init scanner Scanner s = new Scanner(System.in); // scan each word, counting as we go while(s.hasNext()) { // get the next word String word = s.next(); // make a pair Association pair = new Association(word, 1); // find word in vector int loc = words.indexOf(pair); // is this word already in the table? if (loc != -1) { // yes: get the pair already in the table pair = words.get(loc); // update the value in the pair pair.setValue(pair.getValue() + 1); } else { // no: add pair to table words.add(pair); } } for (Association pair : words) { System.out.println("'" + pair.getKey() + "' occurs " + pair.getValue() + " times."); } } }