Java The Complete Reference, Seventh Edition

(Greg DeLong) #1

Thewhileloop stops as soon as the next token is not an integer. Thus, the loop
stops reading integers as soon as a non-integer is encountered in the input stream.
If anextmethod cannot find the type of data it is looking for, it throws a
NoSuchElementException. For this reason, it is best to first confirm that the desired type of
data is available by calling ahasNextmethod before calling its correspondingnextmethod.


Chapter 18: java.util Part 2: More Utility Classes 543


Method Description
String next( ) Returns the next token of any type from the input source.
String next(Patternpattern) Returns the next token that matches the pattern passed
inpatternfrom the input source.
String next(Stringpattern) Returns the next token that matches the pattern passed
inpatternfrom the input source.
BigDecimal nextBigDecimal( ) Returns the next token as aBigDecimalobject.
BigInteger nextBigInteger( ) Returns the next token as aBigIntegerobject. The default
radix is used. (Unless changed, the default radix is 10.)
BigInteger nextBigInteger(intradix) Returns the next token (using the specified radix) as a
BigIntegerobject.
boolean nextBoolean( ) Returns the next token as abooleanvalue.
byte nextByte( ) Returns the next token as abytevalue. The default radix
is used. (Unless changed, the default radix is 10.)
byte nextByte(intradix) Returns the next token (using the specified radix) as a
bytevalue.
double nextDouble( ) Returns the next token as adoublevalue.
float nextFloat( ) Returns the next token as afloatvalue.
int nextInt( ) Returns the next token as anintvalue. The default radix
is used. (Unless changed, the default radix is 10.)
int nextInt(intradix) Returns the next token (using the specified radix) as an
intvalue.
String nextLine( ) Returns the next line of input as a string.
long nextLong( ) Returns the next token as alongvalue. The default radix
is used. (Unless changed, the default radix is 10.)
long nextLong(intradix) Returns the next token (using the specified radix) as a
longvalue.
short nextShort( ) Returns the next token as ashortvalue. The default radix
is used. (Unless changed, the default radix is 10.)
short nextShort(intradix) Returns the next token (using the specified radix) as a
shortvalue.

TABLE 18-16 TheScanner nextMethods

Free download pdf