
// Example program for CISC181, fall 2011
// by Christopher Rasmussen
// University of Delaware

import java.io.*;
import java.util.*;

public class wordbyword {

	public static void main(String[] args) {

		// try to open file to echo

		try {
							
			// set input and output filenames
			
			File inFile = new File("bts.txt");
			FileWriter writer = new FileWriter("echo.txt");
			
			int num_words = 0;
			
			FileReader fileReader = new FileReader(inFile);	
			BufferedReader reader = new BufferedReader(fileReader);
			
			// line by line
		
			ArrayList<String> A = new ArrayList<String>();
			
			while (true) {
				
				// get entire line as one String
				
				String line = reader.readLine();
				
				// end of file?
				
				if (line == null)
					break;
				
				// split line into words using one or more of the following as delimiters: 
				// colon, semicolon, double quote, question mark, exclamation mark, period, hyphen, comma, whitespace
				
				// (this is by no means a complete list--you may want to expand the regular expression)
				
				String[] words = line.split("[:;\"\\?\\!\\.\\-,\\s]+");
			
				// convert array of Strings to ArrayList (not necessary, but handy...)
				
				A.clear();
				Collections.addAll(A, words);
				
				// iterate over words on *this* line, writing each to output file
				
				for (String word : A) {  
					writer.write(Integer.toString(num_words) + ": " + word + "\n");
					num_words = num_words + 1;
				}
				
				// print all of the words on this line for debugging
				
				System.out.println(A);
			}
			
			// don't forget to close the files when done!
			
			reader.close();
			writer.close();
			
			// how many words were processed in total?
			
			System.out.println(num_words + " total words");
		
		} catch (Exception ex) {
			System.out.println("Exception handler!");
			ex.printStackTrace();
		}
	}
}
