File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Scanner Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Reply Bookmark "Scanner" Watch "Scanner" New topic
Author

Scanner

Ankur kothari
Ranch Hand

Joined: Sep 06, 2009
Posts: 531
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

Joined: Sep 06, 2009
Posts: 531
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

Joined: Oct 13, 2009
Posts: 156
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

Sage of The Monstrous Toad of Mount Myoboku
Devaka Cooray
Saloon Keeper

Joined: Jul 29, 2008
Posts: 2691
    
    3

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.


Author of ExamLab (Download) - the free mock exam kit for SCJP / OCPJP
Home Page -- Twitter Profile -- JavaRanch FAQ -- How to Ask a Question
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: Scanner
 
Similar Threads
Scanner(System.in)
Just little Help!
Scanner Class Error!!
To find the Medium
Java 1.5 on mac