Need Help Using Scanner

Write here if you have problems with your Java source code

Moderator: Board moderators

Post Reply
Coalwater
New poster
Posts: 2
Joined: Sat Sep 22, 2007 11:48 pm

Need Help Using Scanner

Post by Coalwater » Wed Sep 26, 2007 3:35 pm

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

bekerpico
New poster
Posts: 2
Joined: Wed Aug 02, 2006 1:08 am
Location: Cairo
Contact:

Re: Need Help Using Scanner

Post by bekerpico » Tue Jun 24, 2008 5:55 pm

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 .
PicO's reply

User avatar
andmej
Experienced poster
Posts: 158
Joined: Sun Feb 04, 2007 7:45 pm
Location: Medellin, Colombia

Re: Need Help Using Scanner

Post by andmej » Wed Jun 25, 2008 12:54 am

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.
Runtime errors in Pascal are reported as Wrong Answers by the online judge. Be careful.

Are you dreaming right now?
http://www.dreamviews.com

mf
Guru
Posts: 1244
Joined: Mon Feb 28, 2005 4:51 am
Location: Zürich, Switzerland
Contact:

Re: Need Help Using Scanner

Post by mf » Wed Jun 25, 2008 7:02 am

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.

Post Reply

Return to “Java”