// Counts the number of midterm scores that were above average import java.util.*; import java.io.*; public class AboveAverage { public static void main(String[] args) throws FileNotFoundException { Scanner input = new Scanner(new File("18wi-mid-scores.txt")); // We can look in the data file and determine exactly how many spaces // we need in the array, but in this program, we just picked a capacity // that was larger than the number of tests. We simply don't use // the extra spaces in the array. This is somewhat bad style. int[] scores = new int[1000]; int sum = 0; int count = 0; while(input.hasNextLine()) { String line = input.nextLine(); Scanner tokens = new Scanner(line); // if (tokens.hasNextInt()) -- not necessary because the input file // is guaranteed to have an int as the first token int actual = tokens.nextInt(); scores[count] = actual; sum += actual; count++; } // sum should be set to the sum of all the scores // count should be the count of scores double average = 1.0 * sum / count; System.out.println("Average was : " + average); // how many were above average? cumulative count using the scores // we put in the array, compared to the calculated average int aboveAverage = 0; for (int i = 0; i < count; i++) { if (scores[i] > average) { aboveAverage++; } } System.out.println("Above average: " + aboveAverage); System.out.println("Count : " + count); } }