Java Reference
In-Depth Information
Table 10-3. Scanner methods
Returned
type
“has” method
“next” meth-
od
Comment
The next complete token from this scanner
String
hasNext()
next()
The next string that matches the given regular expres-
sion (regex)
String
hasNext(Pattern)
next(Pattern)
The next token that matches the regex pattern construc-
ted from the specified string
String
hasNext(String)
next(String)
BigDecimal hasNextBigDecimal() nextBigDecimal() The next token of the input as a BigDecimal
BigInteger hasNextBigInteger() nextBigInteger() The next token of the input as a BigInteger
The next token of the input as a boolean
boolean
hasNextBoolean()
nextBoolean()
The next token of the input as a byte
byte
hasNextByte()
nextByte()
The next token of the input as a double
double
hasNextDouble()
nextDouble()
The next token of the input as a float
float
hasNextFloat()
nextFloat()
The next token of the input as an int
int
hasNextInt()
nextInt()
N/A
Reads up to the end-of-line, including the line ending
String
nextLine()
The next token of the input as a long
long
hasNextLong()
nextLong()
The next token of the input as a short
short
hasNextShort()
nextShort()
The Scanner class is constructed with an input source, which can be a File object
( Chapter 11 ) , an InputStream , a String , or Readable ( Readable is an interface that Read-
er and all its subclasses implement).
One way to use the Scanner is based on the Iterator pattern, using while (scan-
ner.hasNext()) to control the iteration. Example 10-5 shows the simple calculator from
Scanning Input with StreamTokenizer rewritten [ 36 ] to use the Scanner class.
Example 10-5. Simple calculator using java.util.Scanner
 
Search WWH ::




Custom Search