import java.io.*; import java.util.*; class Average13 { // A program to calculate the average of a series of integers // read from a file (may have several integers per line). public static void main(String[] args) throws IOException { int sum=0,count=0; String filename=""; BufferedReader file,in = new BufferedReader(new InputStreamReader(System.in)); for(;;) try { System.out.print("Enter file name: "); filename=in.readLine(); file = new BufferedReader(new FileReader(filename)); break; } catch(FileNotFoundException e) { System.out.println("File "+filename+" not found"); } try { for(;;) { StringTokenizer tokens = new StringTokenizer(file.readLine()); for(; tokens.hasMoreTokens(); count++) sum+=Integer.parseInt(tokens.nextToken()); } } catch(NumberFormatException e) { System.out.println("File contains non-numerical data"); } catch(Exception e) { System.out.println("Average is "+sum/count); } } }