Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Scanner

 
Ankur kothari
Ranch Hand
Posts: 531
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
why doesnt this run?

WHAT DOES THIS MEAN? Scanner s=new Scanner(System.in) ....i want to scan what the use has entered....i can use the console method readline here..
 
Ankur kothari
Ranch Hand
Posts: 531
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
if i removed while and simply used used s.next() i get the answer.....but why does the while method go on and on?
 
rohan yadav
Ranch Hand
Posts: 156
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am not sure abt your problem, but i think problem is in System.in argument which is passed to Scanner constructor.
I run the same program using the String as argument to constructor it works fine, same goes for command line arguments

I have done following modification



And i got output as
Rohan Yadav rwr
 
Devaka Cooray
ExamLab Creator
Marshal
Pie
Posts: 4682
331
Chrome Eclipse IDE Google App Engine IntelliJ IDE jQuery Postgres Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
From the documentation
Scanner.hasNext() Returns true if this scanner has another token in its input. This method may block while waiting for input to scan.

Since you specified System.in as the input source for the scanner, the hasNext test returns 'true' unless the input stream is closed. There are no usage of using the hasNext() method here with System.in as the hasNext() doesn't know whether the user *will* input something further or not. In other words, each invocation of next() method requires a lookup on the specified input (here System.in) that causes the user to input something again.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic