Java Reference
In-Depth Information
Display 10.6
Methods in the Class Scanner (part 4 of 4)
public String nextLine()
Returns the rest of the current input line. Note that the line terminator '\n' is read and discarded;
it is not included in the string returned.
Throws a NoSuchElementException if there are no more lines.
Throws an IllegalStateException if the Scanner stream is closed.
public boolean hasNextLine()
Returns true if there is a next line. May wait for a next line to enter the stream.
Throws an IllegalStateException if the Scanner stream is closed.
public Scanner useDelimiter(String newDelimiter);
Changes the delimiter for input so that newDelimiter will be the only delimiter that separates
words or numbers. See the subsection “Other Input Delimiters” in Chapter 2 for the details. (You
can use this method to set the delimiters to a more complex pattern than just a single string, but we
are not covering that.)
Returns the calling object, but we have always used it as a void method.
Unchecked Exceptions
The exception classes NoSuchElementException , InputMismatchException , and
IllegalStateException are all unchecked exceptions, which means that an exception
of any of these classes is not required to be caught or declared in a throws clause.
Self-Test Exercises
8. Write some code that will create a stream named fileIn that is a member of
the class Scanner . It should connect the stream to a text fi le named sally so
that your program can read input from the text fi le sally .
9. Might the method nextInt in the class Scanner throw an exception? If so,
what type of exception?
10. If the method hasNextInt returns false , does that mean that reading has
reached the end of the fi le?
 
 
Search WWH ::




Custom Search