/* Stats.java * Let's read some numbers from a file, and then find the average. * Assume that the input is in a file called stats.in. */ import java.util.Scanner; // for input import java.io.FileInputStream; // for reading files import java.io.FileNotFoundException; // in case file doesn't exist! public class Stats { public static void main(String [] args) throws FileNotFoundException { Scanner inFile = new Scanner(new FileInputStream("stats.in")); // Need to keep track of how many numbers read, as well as sum. int howMany = 0; double sum = 0.0; // Keep reading numbers until finished reading the file. while(inFile.hasNextDouble()) { double nextNumber = inFile.nextDouble(); howMany = howMany + 1; sum = sum + nextNumber; } // We're done reading the file, so figure the average, and output. double average = sum / howMany; System.out.printf("The average of your %d numbers is %.1f\n", howMany, average); } }