Java Reference
In-Depth Information
23 }
24 }
25 }
Notice that you need to put the throws FileNotFoundException in the header
for main . You don't need to include it in the process method because the code to
open the file appears in method main .
If you put the input data into a file called hours.dat and execute the program,
you get the following result:
Total hours worked by Erica = 42.75
Total hours worked by Erin = 55.75
Total hours worked by Simone = 24.0
Total hours worked by Ryan = 31.75
Total hours worked by Kendall = 5.5
6.3 Line-Based Processing
So far we have been looking at programs that process input token by token. However,
you'll often find yourself working with input files that are line-based: each line of
input represents a different case, to be handled separately from the rest. These types of
files lend themselves to a second style of file processing called line-based processing.
Line-Based Processing
The practice of processing input line by line (i.e., reading in entire lines of
input at a time).
Most file processing involves a combination of line- and token-based styles, and
the Scanner class is flexible enough to allow you to write programs that include both
styles of processing. For line-based processing, you'll use the nextLine and
hasNextLine methods of the Scanner object. For example, here is a program that
echoes an input file in uppercase:
1 // Reads a file and echoes it in uppercase.
2
3 import java.io.*;
4 import java.util.*;
5
6 public class EchoUppercase {
7 public static void main(String[] args)
8 throws FileNotFoundException {
9 Scanner input = new Scanner( new File("poem.txt"));
10 while (input.hasNextLine()) {
 
 
Search WWH ::




Custom Search