
/*************************************************************************
 *  Average: reads in a sequence of real numbers, and prints
 *  their average.
 *
 *  Compilation:  javac Average.java
 *  
 *  Example usage 1:
 *
 *  % java Average
 *    10.0 5.0 6.0
 *    3.0 7.0 32.0
 *    <Ctrl-d>
 *    Average is 10.5
 *
 *  Note: <Ctrl-d> signifies the end of file on Unix/Linux.
 *  On Windows use <Ctrl-c>.
 *
 *  Example usage 2 (uses input redirection):
 *
 *  % java Average < data.txt
 *    Average is 8.3
 *
 *************************************************************************/

import java.util.Scanner;

public class Average { 

    public static void main (String [] args) { 

        Scanner scanner = new Scanner (System.in);

        int nvals = 0;       // Number of input values
        double sum = 0.0;    // Sum of input values

        // read data and accumulate running sum...

        while (scanner.hasNext()) {
            double x = scanner.nextDouble();
            nvals++;
            sum += x;
        }

        double average = sum / nvals;

        System.out.println ("Number  = " + nvals);
        System.out.println ("Average = " + average);
    }
}
