/* Marty Stepp, CSE 142, Spring 2010 This program reads a file of test scores and shows a histogram of the score distribution, with a star for each student that got a given score. It is another example of tallying using an array. Example input: 94 44 66 93 73 ... Example output: ... 87: **** 88: * 89: ***** 90: ******** ... */ import java.io.*; import java.util.*; public class Histogram { public static void main(String[] args) throws FileNotFoundException { int[] counts = new int[101]; // counters of test scores 0 - 100 Scanner input = new Scanner(new File("midterm.txt")); while (input.hasNextInt()) { // read file into counts array int score = input.nextInt(); counts[score]++; // if score is 87, then counts[87]++ } // print star histogram for (int i = 0; i < counts.length; i++) { if (counts[i] > 0) { // counts[i] stores how many students scored i on the test, // so print a star (counts[i]) times System.out.print(i + ": "); for (int j = 0; j < counts[i]; j++) { System.out.print("*"); } System.out.println(); } } } }