Computer Science 210
Data Structures
Fall 2017, 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
*
* $Id$
*
*/
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);
}
}