/** Let's write a simple program that reads a text file and counts how * many words it has. */ import java.io.*; import java.util.*; public class CountWords { public static void main(String [] args) throws IOException { BufferedReader in = new BufferedReader(new FileReader("input.txt")); int numWords = 0; while(true) { String line = in.readLine(); if (line == null) break; StringTokenizer tok = new StringTokenizer(line, " .,;?!"); while (tok.hasMoreTokens()) { tok.nextToken(); ++numWords; } } System.out.printf("I found %d words\n", numWords); } }