import java.util.*;
import java.io.*;

public class FileExample
{
  
// precondition: none
// postcondition: displays average of a list of numbers
//                entered by user using the screen
  public static void averageKeyboardScreen()
  {
    Scanner input = new Scanner(System.in);
    
    int number = 0;
    int sum = 0;
    int size = 0;
    
    // Example of list headed by size input
    
    System.out.print("Enter size: ");
    size = input.nextInt();
    
    for (int i = 0; i < size; i++)
    {
      System.out.print("Enter next number: ");
      number = input.nextInt();
      sum = sum + number;
      
    }
    
    System.out.println("Average is: " + ((double) sum / size));
  }
  
// precondition:  file 'numbers.txt' must exist in program's folder
//                the first number is the number of numbers in the file
//                the rest of the entries are the numbers
// postcondition: writes average of a list of numbers from the file
//                'numbers.txt' to the file 'output.txt'.
  public static void averageFile() 
  {
    int sum = 0;
    
    // This odd bit of syntax creates a "try block".  It's denotes a block of code
    // much like an if block, or do/while block, or method block.  Any code that
    // messes around with files needs to be inside a try block.
    try
    { // <--- Try block starts here
      
       // File must be in same folder as executable
       File infile = new File("numbers.txt");
    
       // File will be created in same folder as executable
       File outfile = new File("output.txt");

      // This scanner will work the same as the scanner that grabs
      // input from the keyboard, but the file "numbers.txt" will
      // be used for input instead.
      Scanner input = new Scanner(infile);
      
      // This output object will act the same as System.out, but 
      // the file "output.txt" will get the output instead.
      PrintWriter output = new PrintWriter (outfile);
        
      int count=0;   
      while (input.hasNext())
      {
        int number = input.nextInt();
        sum = sum + number;
        count++;
      }
      
      output.println("Average is: " + ((double)sum/count));
      
      // close the input and output files
      output.close();
      input.close();
      
    } // <--- Try block ends here
    
    // This odd bit of syntax creates a "catch block".  It denotes a block of code
    // that gets triggered IMMEDIATELY if an error (called an exception) is generated
    // inside the "try block".  We then print out the error message generated by 
    // the error.  A number of file methods will jump to this catch block if an error
    // occurs inside the try block.
    catch (Exception e)
    {
      System.out.println(e.getMessage());
    }
  }
  
  public static void main(String[] args)
  {
    //averageKeyboardScreen();
    averageFile();
  }
}
