Computer Science 210
Data Structures
Fall 2019, Siena College
HoursWorked2 BlueJ Project
Click here to download a BlueJ project for HoursWorked2.
HoursWorked2 Source Code
The Java source code for HoursWorked2 is below. Click on a file name to download it.
/** * Example HoursWorked2: another method example, and using * a Scanner on a String, this time passing the Scanner as a parameter * instead of the 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 HoursWorked2 { /** 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")); // pass the Scanner to the method to process one line // at a time while (input.hasNextLine()) { readAndProcessLine(input); } } /** * Read and rocess the line for one employee with the format: * ID Name hours hours hours ... * * @param s the Scanner from which to read the next employee's line */ public static void readAndProcessLine (Scanner s) { String text = s.nextLine(); // create a Scanner to parse the next line 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); } }