// Illustrates reading from keyboard input in a simple word count program // Adapted from "Just Java" by Peter van Linden. // import java.io.*; import java.util.*; public class wc { public static void main(String[] args) { InputStreamReader is = new InputStreamReader(System.in); BufferedReader br = new BufferedReader(is); StringTokenizer st; int numWords=0; int numLines=0; String line; String word; try { // The basic Java reading routines can all throw IOExceptions do { line = br.readLine(); // read a line if (line != null) { // null at end of file numLines++; st=new StringTokenizer(line); while (st.hasMoreTokens()) { word = st.nextToken(); // take a token off the line numWords++; } // end loop over words } // end if } while (line != null); // end loop over lines System.out.println("Words: " + numWords + " Lines: " + numLines); } catch (IOException ioe) { System.out.println("IO error" + ioe); } } }