Java Reference
In-Depth Information
There are two while loops in this program, one nested within the other. The
outer loop processes each line in the file, and the inner loop processes each token
in the current line.
The variable fileScan is created as a scanner that operates on the input file
named urls.inp . Instead of passing System.in into the Scanner constructor, we
instantiate a File object that represents the input file and pass it into the Scanner
constructor. At that point, the fileScan object is ready to read and process input
from the input file.
If for some reason there is a problem finding or opening the input file, the
attempt to create a File object will throw an IOException , which is why we've
added the throws IOException clause to the main method header. (Processing I/O
exceptions is discussed further in Chapter 11.)
The body of the outer while loop will be executed as long as the hasNext
method of the input file scanner returns true—that is, as long as there is more
input in the data file to process. Each iteration through the loop reads one line
(one URL) from the input file and prints it out.
For each URL, a new Scanner object is set up to parse the pieces of the URL
string, which is passed into the Scanner constructor when instantiating the urlScan
object. The inner while loop prints each token of the URL on a separate line.
Recall that, by default, a Scanner object assumes that white space
(spaces, tabs, and new lines) is used as the delimiters separating the
input tokens. That works in this example for the scanner that is reading
each line of the input file. However, if the default delimiters do not suf-
fice, as in the processing of a URL in this example, they can be changed.
KEY CONCEPT
The delimiters used to separate
tokens in a Scanner object can be
explicitly set as needed.
In this case, we are interested in each part of the path separated by the slash ( / )
character. A call to the useDelimiter method of the scanner sets the delimiter to
a slash prior to processing the URL string.
If you want to use more than one alternate delimiter character, or if you want
to parse the input in more complex ways, the Scanner class can process patterns
called regular expressions, which are discussed in Appendix H.
SELF-REVIEW QUESTIONS (
)
SR 5.24 Devise statements that create each of the following Scanner objects.
see answers in Appendix N
a. One for interactive input, which reads from System.in .
b. One that reads from the file “info.dat”.
c. One that reads from the String variable infoString .
SR 5.25 Assume the Scanner object fileScan has been initialized to read from
a file. Write a while loop that calculates the average number of char-
acters per line of the file.
 
Search WWH ::




Custom Search