The try
-with-resources statement opens the file for reading by instantiating a Scanner
object (line 14). We pass a Path
object to the constructor, which specifies that the Scanner
object will read from the file "clients.txt"
located in the directory from which the application executes. If the file cannot be found, an IOException
occurs. The exception is handled in lines 25–28.
Lines 15–16 display headers for the columns in the application’s output. Lines 19–23 read and display the file’s content until the end-of-file marker is reached—in which case, method hasNext
will return false
at line 19. Lines 21–22 use Scanner
methods nextInt
, next
and nextDouble
to input an int
(the account number), two String
s (the first and last names) and a
Get Java How to Program, Early Objects, 11th Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.