// CSE 142 Lecture 14 // Files // Reads a file of TA hours and figures out how many hours each // TA worked and their average hours worked per day. import java.io.*; // for File, FileNotFoundException import java.util.*; // for Scanner public class HoursWorked { public static void main(String[] args) throws FileNotFoundException { Scanner input = new Scanner(new File("hours.txt")); while (input.hasNextLine()) { String line = input.nextLine(); processLine(line); } } // Process 1 TAs hours. Figure out their hours worked and // average hours worked per day and print those stats. public static void processLine(String line) { Scanner lineScan = new Scanner(line); int id = lineScan.nextInt(); String name = lineScan.next(); double totalHours = 0.0; int days = 0; while (lineScan.hasNextDouble()) { totalHours += lineScan.nextDouble(); days++; } System.out.printf("%s (ID#%d) worked %.1f hours (%.2f hours/day)\n", name, id, totalHours, totalHours / days); } }