Java Reference
In-Depth Information
if (ex!= null)
throw ex;
return vals;
}
Here the delimiter pattern is either a single comma or a line separator,
since the values themselves can contain any other characters, including
whitespace. The definition of a line separator is documented in the
Pat-
tern
class. To make it easier to read the example, we defined a constant
to represent that pattern:
static final String LINE_SEPARATOR_PATTERN =
"\r\n|[\n\r\u2028\u2029\u0085]";
The
Scanner
class also provides a
close
method. When you invoke
close
on a scanner, the input source will be closed if it is
Closeable
. Once a
scanner has been closed, attempts to invoke any of the scanning op-
erations will result in an
IllegalStateException
. Take care when closing
a scanner: The input source may not be yours to close, such as an
In-
putStreamReader
for
System.in
. As a general rule, don't close a scanner
unless you opened its input source.
22.5.2. Scanning Lines
In line mode, the scanner can process a line at a time using the regular
expression pattern that you supply. If the line matches the pattern
then a
java.util.regex.MatchResult
object can be obtained from the
match
method, from which you can extract the groups the pattern defined. Line
matching is done with the
findInLine
method.
public String findInLine(Pattern pattern)