/* Marty Stepp, CSE 142, Spring 2010 This program reads an input file of TAs' hours worked and outputs each TA's total hours for the week and hours per day. Input file hours.txt: 123 Kim 12.5 8.1 7.6 3.2 456 Eric 4.0 11.6 6.5 2.7 12 1.5 2.6 789 Stef 8.0 8.0 8.0 8.0 7.5 Expected output (to file hours_out.txt): Kim (ID#123) worked 31.4 hours (7.85 hours/day) Eric (ID#456) worked 36.8 hours (7.36 hours/day) Stef (ID#789) worked 39.5 hours (7.9 hours/day) */ import java.io.*; // for File import java.util.*; public class Hours2 { public static void main(String[] args) throws FileNotFoundException { Scanner input = new Scanner(new File("hours.txt")); PrintStream output = new PrintStream(new File("hours_out.txt")); // read each person from the file while (input.hasNextLine()) { String line = input.nextLine(); onePerson(line, output); } } // Reads one TA's hours data from the given line and outputs // the resulting data to the given output stream. public static void onePerson(String line, PrintStream output) { // line = "123 Kim 12.5 8.1 7.6 3.2" // lineScanner ^ Scanner lineScanner = new Scanner(line); int id = lineScanner.nextInt(); String name = lineScanner.next(); // read the hours for each day double totalHours = 0.0; int days = 0; while (lineScanner.hasNextDouble()) { double hours = lineScanner.nextDouble(); totalHours += hours; days++; } double average = totalHours / days; // output the information to the file // (to control the decimal precision, use output.printf) output.println(name + " (ID#" + id + ") worked " + totalHours + " hours (" + average + " hours/day)"); } }