Java Reference
In-Depth Information
The data are line-oriented, each employee's information appearing on a different
line of input, but this aspect of the data wasn't incorporated into the program. The
program processed the file in a token-based manner. However, that approach won't
always work. For example, consider a slight variation in the data in which each line
of input begins with an employee ID rather than just a name:
101 Erica 7.5 8.5 10.25 8 8.5
783 Erin 10.5 11.5 12 11 10.75
114 Simone 8 8 8
238 Ryan 6.5 8 9.25 8
156 Kendall 2.5 3
This addition seems like a fairly simple change that shouldn't require major
changes to the code. Recall that the program uses a method to process the file:
public static void process(Scanner input) {
while (input.hasNext()) {
String name = input.next();
double sum = 0.0;
while (input.hasNextDouble()) {
sum += input.nextDouble();
}
System.out.println("Total hours worked by " + name +
" = " + sum);
}
}
Suppose you add a line of code to read the employee ID and modify the println
to report it:
public static void process(Scanner input) {
while (input.hasNext()) {
int id = input.nextInt();
String name = input.next();
double sum = 0.0;
while (input.hasNextDouble()) {
sum += input.nextDouble();
}
System.out.println("Total hours worked by " + name +
" (id#" + id + ") = " + sum);
}
}
Search WWH ::




Custom Search