Java Reference
In-Depth Information
16 System.out.println(" hasNext = " + console.hasNext());
17 }
18 }
Let's look at a few sample executions. Here is the output of the program when we
enter the token
348
:
This program examines the ways
a token can be read.
token?
348
hasNextInt = true
hasNextDouble = true
hasNext = true
As you'd expect, the call on
hasNextInt
returns
true
, which means that we
could interpret this token as an integer. The
Scanner
would also allow us to interpret
this token as a
double
, so
hasNextDouble
also returns
true
. But notice that
hasNext()
returns
true
as well. That result means that we could call the
next
method to read in this token as a
String
.
Here's another execution, this time for the token
348.2
:
This program examines the ways
a token can be read.
token?
348.2
hasNextInt = false
hasNextDouble = true
hasNext = true
This token cannot be interpreted as an
int
, but it can be interpreted as a
double
or a
String
. Finally, consider this execution for the token
hello
:
This program examines the ways
a token can be read.
token?
hello
hasNextInt = false
hasNextDouble = false
hasNext = true
The token
hello
can't be interpreted as an
int
or
double
; it can only be inter-
preted as a
String
.
Search WWH ::
Custom Search