import java.io.*; import java.text.*; // //File Input and Output (I/O) // // public class program8 { public static double Average(String filename,int max) throws IOException { //precondition: filename of doubles is passed in, number //of doubles in the file is passed int //postcondition: average is returned BufferedReader in= new BufferedReader(new FileReader(filename)); double sum=0,number=0; for(int count=1;count<=max;count++) { number=Double.valueOf(in.readLine().trim()).doubleValue(); sum=sum+number; } in.close(); return sum/max; } public static void main(String[] args) throws IOException { String filename; int max=0;//number of values in file //create input stream to read from keyboard BufferedReader in=new BufferedReader (new InputStreamReader(System.in)); System.out.println("This program reads in numbers"+ " from a file and averages them.\n"); System.out.println("Please enter the filename: "); filename=in.readLine(); System.out.println("Please enter the number of values: "); max=Integer.parseInt(in.readLine()); double ave=Average(filename,max);//function call System.out.println("Average is: "+ave); System.out.println("\nProgram Over, Bye!!!\n"); } }