• 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:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Help needed with Scanner

 
Ranch Hand
Posts: 103
Netbeans IDE Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am trying to run this following code using scanner to obtain the tokens:



However when i am taking the input from the console using the Scanner, the while loop correctly parses the tokens, but then it keeps on looping in endless loop, and the program execution does not stop.

If i change the Scanner object to take the input from command line i.e


The while loop correctly terminates after parsing out the tokens.

can you please explain why the while loop does not terminate when i am taking input from the user, and is there a way to make the while loop correctly terminate while doing the same
 
Ranch Hand
Posts: 441
Scala IntelliJ IDE Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
System.in is a stream, so Scanner has to wait to see what comes in. If it finished as soon as there was a break in the stream, it wouldn't wait for you to type anything in. So while the stream exists, it assumes there is something else coming. If you just want to wait for 1 token, just remove the while loop and the code should work.
 
jishnu dasgupta
Ranch Hand
Posts: 103
Netbeans IDE Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is there any way that i can place some condition in my while loop, so that after parsing the tokens in my stream the program exists.
say for example, if i enter the input string as: "Hello i am jishnu";

it parses the String and then the program exists.
 
Luigi Plinge
Ranch Hand
Posts: 441
Scala IntelliJ IDE Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try Scanner's nextLine() method, which will return the whole line, and use String.split on that.
 
Marshal
Posts: 80616
468
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Luigi Plinge wrote:Try Scanner's nextLine() method, which will return the whole line . . .

Not quite. It returns whatever remains until the next line end. There is lots of potential for confusion about that.
 
jishnu dasgupta
Ranch Hand
Posts: 103
Netbeans IDE Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Luigi,

It didnt work with the nextLine()....is there any alternative??
 
Luigi Plinge
Ranch Hand
Posts: 441
Scala IntelliJ IDE Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Works fine for me -
 
jishnu dasgupta
Ranch Hand
Posts: 103
Netbeans IDE Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oh!!...I had that String.split() condition wrong...Thanks anyways!!!...
 
reply
    Bookmark Topic Watch Topic
  • New Topic