//File:	AverageFile.java
import CSLib.*;
import java.io.*;
public class AverageFile
{
	public static void main (String [] args)
	{
		int [] list = new int [100];
    		
    		int k=0;
		
		//Step 1.  file (stream of bytes)
		String filename = "ints.txt";
			
    		try
    		{
			//Step 2.  FileReader object (stream of chars)
			FileReader fr = new FileReader(filename); //throws FileNotFoundException
			
			//Step 3.  BufferedReader object (buffers input allowing access from RAM instead of file)
			BufferedReader br = new BufferedReader (fr);
		
			//Step 4.  readLine() message to Buffered Reader object returns String reference to first line of input
			String line = br.readLine(); //throws IOException
     			while (line != null)
     			{
     				try
     				{
     					list [k] = Integer.parseInt (line); //Step 6: converts String to int
     				}
     				catch (NumberFormatException e)
     				{
     					System.out.println ("Error in input. ");
     					System.out.println (line);
     				}

     				k++;
     				line = br.readLine();
     			}
     			
     			br.close(); //not invoked if throws exception
     				
    		}
    		catch (FileNotFoundException e)
    		{
    			System.out.println ("The file " + filename + " was not found.");
    		}
    		catch (IOException e)
    		{
    		}
    		
		OutputBox out = new OutputBox("Averaging List");
		out.setSize(280, 250);
		out.println ("Number");
		
		int sum=0;
		for (int j=0; j<k; j++)
		{
			sum = sum + list[j];
			out.println ("" + list[j]);
		}
		
		out.println ("\nThe average of the list is: " + sum/k);
  	}
}
