// This program searches an input file of employees' hours worked // for a particular employee and outputs that employee's hours data. import java.io.*; // for File import java.util.*; // for Scanner public class HoursWorked { public static void main(String[] args) throws FileNotFoundException { Scanner console = new Scanner(System.in); System.out.print("Enter a name: "); String searchName = console.next(); // "BRAD" // int x = 0; boolean found = false; // a boolean flag Scanner input = new Scanner(new File("hours.txt")); while (input.hasNextLine()) { String line = input.nextLine(); // "456 Brad 4.0 11.6 6.5 2.7 12" Scanner lineScan = new Scanner(line); int id = lineScan.nextInt(); // 456 String name = lineScan.next(); // "Brad" if (name.equalsIgnoreCase(searchName)) { // x = 1; found = true; // found him! // if (name.equals(searchName)) { double sum = 0.0; int count = 0; while (lineScan.hasNextDouble()) { double hours = lineScan.nextDouble(); sum += hours; count++; } double average = sum / count; System.out.println(name + " (ID#" + id + ") worked " + sum + " hours (" + average + " hours/day)"); } } // flag will be true if we ever found the person, false if not // if (found == false) { if (!found) { System.out.println("Not found!"); } } }