Page 1 of 1

Need Help Using Scanner

Posted: Wed Sep 26, 2007 3:35 pm
by Coalwater
i have a problem with Scanner (java.util.Scanner)
its in using Scanner.hasNext() while reading from console
the problem is that the program never stops and i get time limit exceed
cant i use hasNext() when the Scanner is scanner(System.in) ?

Thank you

Re: Need Help Using Scanner

Posted: Tue Jun 24, 2008 5:55 pm
by bekerpico
Yes , you can . but you can't test it in your compiler, because you have to make the input has an end and you can't do that while it's waiting input from keyboard while the automatic judge can do .

Re: Need Help Using Scanner

Posted: Wed Jun 25, 2008 12:54 am
by andmej
Actually, if you are using Linux you can. Just press Ctrl + D at a console waiting for input and it will send the EOF signal to the process. If you are using Windows, download a Linux distro for free.

Re: Need Help Using Scanner

Posted: Wed Jun 25, 2008 7:02 am
by mf
Windows's equivalent of Ctrl-D is Ctrl-Z.

Alternatively, you can force your program to read from a real file using redirection, just type in the command line:

Code: Select all

java Main <input_file >output_file
(provided that you've called the main class just "Main")

This works under every OS I know.