import java.io.*; class Average9 { /* A program that calculates the average of a series of integers read from a file. A sentinel value is used to indicate the end of the series. The program prompts for the name of the file the numbers are to be read from. */ static final int SENTINEL = -999; public static void main(String[] args) throws IOException { BufferedReader in = Text.open(System.in); BufferedReader fileReader; String filename; int sum=0,count=0,n; System.out.print("Enter file name to read from: "); filename=Text.readString(in); try { fileReader=Text.open(filename); n=Text.readInt(fileReader); while(n!=SENTINEL) { sum+=n; count++; n=Text.readInt(fileReader); } } catch(EOFException e) { System.out.println("No sentinel "+SENTINEL+" found in file"); } catch(FileNotFoundException e) { System.out.println("File "+filename+" not found"); } System.out.println("Average is: "+Text.writeDouble((double)sum/count,5,3)); } }