Computer Science 210
Data Structures
Fall 2019, Siena College
HoursWorked BlueJ Project
Click here to download a BlueJ project for HoursWorked.
HoursWorked Source Code
The Java source code for HoursWorked is below. Click on a file name to download it.
/** * Example HoursWorked: another method example, and using * a Scanner on a String * * @author Siena College, CSIS 120 faculty * Updated by Jim Teresco, CSC 202, The College of Saint Rose, Fall 2012 * @version Fall 2019 * */ import java.io.File; import java.io.IOException; import java.util.Scanner; public class HoursWorked { /** Read some information about hours worked by employees and print the total hours worked by each. @param args not used @throws IOException if there are problems reading the hours.dat file */ public static void main(String[] args) throws IOException { Scanner input = new Scanner (new File("hours.dat")); // read the entire line to be parsed by processLine while (input.hasNextLine()) { String text = input.nextLine(); processLine(text); } } /** * Process the line for one employee with the format: * ID Name hours hours hours ... * * @param text the String for one employee's hours */ public static void processLine (String text) { // create a Scanner to parse the String Scanner data = new Scanner(text); // we know the String contains an int int id = data.nextInt(); // then a String String name = data.next(); double sum = 0.0; // then a series of doubles while (data.hasNextDouble()) { sum += data.nextDouble(); } System.out.println("Total hours worked by " + name + " (id #" + id + ") = " + sum); } }