I need to solve the following problem: Write a method named tokenStats that accepts as a parameter a Scanner containing a series of tokens. It should print out the sum of all the tokens that are legal integers, the sum of all the tokens that are legal real numbers but not integers, and the total number of tokens of any kind. For example, if a Scanner called data contains the following tokens:
3 3.14 10 squid 10.x 6.0
Then the call of tokenStats(data)
; should print the following output:
integers: 13
real numbers: 9.14
total tokens: 6
If the Scanner has no tokens, the method should print:
integers: 0
real numbers: 0.0
total tokens: 0
So, this is my question. I have tried to use
while (input.hasNext()) {
if (input.hasNextInt()) {
and this creates an infinite loop,
but if I use
while (input.hasNext()) {
input.next();
if (input.hasNextInt()) {
I lose my first token if it is an int...
what should I do?
I suggest you check this way .. which cover all your scenario