How do I read input from a stream "one word at a time"?

John Zukowski

What you need to do is use the java.util.StringTokenizer or java.io.StreamTokenizer to parse your input into words. Each has a default set of delimiters like white space that can be changed. The following demonstrates the using of StringTokenizer to count words in a file.

import java.io.*;
import java.util.*;

public class Test {
  static final Integer ONE = new Integer(1);

  public static void main (String args[]) 
      throws IOException {

    Map map = new TreeMap();
    FileReader fr = new FileReader(args[0]);
    BufferedReader br = new BufferedReader(fr);
    String line;
    while ((line = br.readLine()) != null) {
      processLine(line, map);
    }
    printMap(map);    
  }
  static void processLine(String line, Map map) {
    StringTokenizer st = new StringTokenizer(line);
    while (st.hasMoreTokens()) {
      addWord(map, st.nextToken());
    }
  }
  static void addWord(Map map, String word) {
    Object obj = map.get(word);
    if (obj == null) {
      map.put(word, ONE);
    } else {
      int i = ((Integer)obj).intValue() + 1;
      map.put(word, new Integer(i));
    }
  }
  static void printMap(Map map) {
    Set set = map.entrySet();
    Iterator it = set.iterator();
    while (it.hasNext()) {
      Map.Entry entry = (Map.Entry)it.next();
      System.out.println(entry.getKey() + 
        ": " + entry.getValue());
    }
  }
}
0 Comments  (click to add your comment)
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

About | Sitemap | Contact