// CSE 142, Summer 2008 (Marty Stepp) // This program processes a file of employee data and outputs // each employee's hours worked and hours per day. // // The program demonstrates line-based file processing and using // a Scanner to tokenize the contents of each individual line. import java.io.*; // File import java.util.*; // Scanner public class Hours { public static void main(String[] args) throws FileNotFoundException { Scanner input = new Scanner(new File("hours.txt")); while (input.hasNextLine()) { String line = input.nextLine(); // "123 Susan 12.5 8.1 7.6 3.2" Scanner lineScanner = new Scanner(line); int id = lineScanner.nextInt(); String name = lineScanner.next(); // cumulative sum of all hours worked by this employee double sum = 0.0; int days = 0; while (lineScanner.hasNextDouble()) { sum = sum + lineScanner.nextDouble(); days++; } System.out.println(name + " (ID#" + id + ") worked " + sum + " hours (" + (sum/days) + " / day)"); } } }