/** * WordCounter1.java * * @author Jeff Ondich * @version 9/13/05 * * Counts the words in the standard input stream (System.in). * To use this program effectively, you'll want to "redirect" * System.in to a file, like so: * * java WordCounter1 < yourfile.txt * * This command line structure with the < tells the java program * to attach System.in to the file yourfile.txt rather than to * the keyboard as usual. */ import java.util.*; import java.io.*; public class WordCounter1 { public static void main( String[] args ) { int nWords = 0; Scanner in = new Scanner( System.in ); while( in.hasNext() ) { nWords++; System.out.println( in.next() ); } System.out.println(); System.out.println( "There were " + nWords + " \"words\" in the input." ); } }