/** * WordCounter2.java * * @author Jeff Ondich * @version 9/13/05 * * Counts the words in the file specified on the command line. * This is the same as WordCounter1.java, but uses a File * object rather than System.in redirected to a file. */ import java.util.*; import java.io.*; public class WordCounter2 { public static void main( String[] args ) throws FileNotFoundException { int nWords = 0; Scanner in = new Scanner( new File( args[0] ) ); while( in.hasNext() ) { nWords++; System.out.println( in.next() ); } System.out.println(); System.out.println( "There were " + nWords + " \"words\" in the input." ); } }