This week's giveaway is in the Spring forum.
We're giving away four copies of REST with Spring (video course) and have Eugen Paraschiv on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Hum, what's wrong here? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of REST with Spring (video course) this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Hum, what Watch "Hum, what New topic

Hum, what's wrong here?

Jesse Crockett
Ranch Hand

Joined: Feb 03, 2005
Posts: 129
Here is the output:

Ulf Dittmer

Joined: Mar 22, 2005
Posts: 42959
I haven't tried to use Scanner with, but my guess would be that the call to nextInt expects a number to be available already, and since there is none, it throws an exception. In other words, the call doesn't wait until there is an int. Have a look at the hasNextInt method to see whether there actually is an int available.

And please UseAMeaningfulSubjectLine
[ January 13, 2007: Message edited by: Ulf Dittmer ]
Jim Yingst

Joined: Jan 30, 2000
Posts: 18671
In general, nextInt() would block waiting for new input. The problem here is that has already been closed, near the top of the method. Closing the scanner also closes the stream it's wrapped around. Once it's been closed, you can't reopen it. So when you later create a new Scanner wrapped around, it can't possibly read anything - hence, the error you've gotten.

If you want to use more than once, you need to create a single Scanner for reading, and keep using it. Don't close it until you're really done with it, and don't try to create a new one.

"I'm not back." - Bill Harding, Twister
Jesse Crockett
Ranch Hand

Joined: Feb 03, 2005
Posts: 129
I agree. Here's the link:
subject: Hum, what's wrong here?
It's not a secret anymore!