• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Hum, what's wrong here?

 
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here is the output:




 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I haven't tried to use Scanner with System.in, 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 ]
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In general, nextInt() would block waiting for new input. The problem here is that System.in 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 System.in, it can't possibly read anything - hence, the error you've gotten.

If you want to use System.in more than once, you need to create a single Scanner for reading System.in, and keep using it. Don't close it until you're really done with it, and don't try to create a new one.
 
Jesse Crockett
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks
 
I can't take it! You are too smart for me! Here is the tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic