/*
	Tom DeDonno page 595

	WordCount usage:

	WordCount -f filename [-l]

	if -l option is used program 
	prints each word in file one per line

	at end program number of words read is displayed

	Assignment - Program Modifications

        drop -l option 
        modify tokenizer to take into account punctuation =;().{}+-" etc.
	create output file with just words
        program should output characters for all words, words  and lines read
	if -f filename is not used program should print a warning message
	
*/

import java.io.*; //for BufferedReader and FileReader
import java.util.StringTokenizer;

public class WordCount {


	public static void main( String[] args ) {

		boolean fWords = false;
		String fileName = (String)null;
		int i, words, lines, characters;

	        for( i=0; i < args.length ; ++i ) {
                        System.out.println( "args = " + args[i] + "args.length" +
                  args.length );
			if( args[i].equalsIgnoreCase( "-l" ) ) fWords = true;
			if( args[i].equalsIgnoreCase( "-f" ) ) 
				fileName = args[i+1];
                        
		} //End for
			
                words = 0;
		try {

			// Can fileName declaration be put here?
			
			BufferedReader inputStream =
			new BufferedReader( new FileReader( fileName ) );
                        //Throws FileNotFoundException

                        String Line = (String)null;
			while( (Line=inputStream.readLine()) != (String)null ) {

				StringTokenizer wordFinder =
				new StringTokenizer( Line );

				words += wordFinder.countTokens();
				if( fWords ) 
					while( wordFinder.hasMoreTokens() )
						System.out.println( 
						wordFinder.nextToken() );
			} //end while( (Line=Input
				
			inputStream.close( );
		
		} catch( FileNotFoundException e ) {
		System.out.println( "File " + fileName + " was not found");
		System.out.println( " or could not be opened" );
		
		} catch( IOException e ) {
		System.out.println( "Error reading from file " + fileName );
		
		} finally {
			System.out.println( "Words = " + words );
		}

	} //End main
} //end class

		 
