import java.io.File;
import java.util.Scanner;

// WordCount application
// public class WordCount {
public class Question1 {

	public static void main(String[] args) {
		
		// Instantiate File and Scanner objects
		File foobar;
		Scanner input;
		
		// For quick test
		// A more meaningful text file name than just "source.txt"
		//foobar = new File("files\\Question1-source.txt.txt");
		
		// For full test
		// "Call of the Wild" by Jack London, via Project Guttenberg
		// One of my favourite short stories ; )
		foobar = new File ("files\\callw10.txt");
		
		// Read file
		try {
		
			// Set up Scanner and delimiter
			input = new Scanner(foobar);
			input.useDelimiter("(\\W+)"); // Non-word characters
			
			// Setup variables
			String buffer;
			int wordCount = 0, letterCount = 0;
			
			// Process words
			System.out.println( "==================================="
					+ "\nStart processing text file " + "\"" + foobar + "\""
					+ "\n===================================\n"
					);
			while (input.hasNext()) { // Definted by delimiter
				
				// Next word to buffer
				buffer = input.next();
				
				// Increase counters
				wordCount++;
				letterCount+=buffer.length();
				
				// Print out progress
				System.out.println("Buffer Word:\t" + buffer
						+ "\nWord Count:\t" + wordCount
						+ "\nLetter Count:\t" + letterCount
						+ "\n");
				
			}
			
			// Final stats
			System.out.println( "==================================="
					+ "\nTotal number of words:\t" + wordCount
					+ "\nTotal letter count:\t" + letterCount
					+ "\nAverage word length:\t" + (float)(letterCount)/(float)(wordCount)
					// or (for rounded integer value)
					//+ "\nAverage word length:\t" + (letterCount+(wordCount/2))/wordCount
					+ "\nDone processing text file " + "\"" + foobar + "\""
					+ "\n===================================\n"
					);
			
			// Clean up
			input.close();
			
		} catch (java.io.FileNotFoundException e) {
			
			// From "input = new Scanner(foobar);"
			System.err.println("Error: file " + foobar + " not found.");
			e.getMessage();
			System.exit(1);
		}
		

	}

}
