//This program goes through the file midterm1.txt line by line. // Each line has a score range followed by all the scores that were // recieved in that range. For example: // 40–49 42 42 // This line has two scores, both 42 that were recieved in the // range 40-49. // This program counts the scores and outputs a line for each range // showing the range and the number of scores. For example: // 40–49: 2 import java.io.*; import java.util.*; public class CountGrades { public static void main(String[] args) throws FileNotFoundException { //Create a file scanner on the file midterm1.txt Scanner fileScan = new Scanner(new File("midterm1.txt")); // a while loop to go find each line in the file while(fileScan.hasNext()){ //while there are still tokens in the file // get the line String line = fileScan.nextLine(); // create a scanner to let us loop over the tokens in that line Scanner lineScan = new Scanner(line); // the first token in the line is the range String range = lineScan.next(); // count the remaining tokens in the line int scoreCount = 0; while(lineScan.hasNext()){ //while there are still tokens in the line //pull off a score and don't save it (we are only counting) lineScan.nextInt(); scoreCount++; // add one to the count since we just pulled off a score } // for each line we print the range and the count of scores System.out.println(range + ": " + scoreCount); } } }