reset password
Author Message
Doublenyher
Posts: 5
Posted 18:09 Dec 09, 2013 |

How do I use .hasNext(), .hasNextInt(), and .hasNextDouble() without it ending as an infinite loop?

abet
Posts: 5
Posted 18:10 Dec 09, 2013 |

Can you say more about why you think it might produce an infinite loop. Do you have an example program that you are concerned about?

Doublenyher
Posts: 5
Posted 18:14 Dec 09, 2013 |
abet wrote:

Can you say more about why you think it might produce an infinite loop. Do you have an example program that you are concerned about?

I'm using it like this:

Scanner in = new Scanner(System.in);

while (in.hasNextDouble())
{

     integer++;

}

Last edited by Doublenyher at 18:15 Dec 09, 2013.
Eric Liao
Posts: 158
Posted 19:06 Dec 09, 2013 |

You can probably use hasNext() first and get the String and cast the String to the integer, double, or String later. However, be aware of casting the String to integer or double.