Computer Science 202
Introduction to Programming

Fall 2013, The College of Saint Rose

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.


HoursWorked.java

/**
 * 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
 * 
 * $Id: HoursWorked.java 1976 2012-11-08 03:41:25Z terescoj $
 *
 */

import java.io.File;
import java.io.IOException;
import java.util.Scanner;

public class HoursWorked {

    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 ...
    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);
    }
}