Page 1 of 1

Code with using util.Scanner.nextInt() complies but not run

Posted: Fri Dec 07, 2007 11:47 pm
by mals@nash-inc.com
Hi:

I have Java6 intalled.

Using TextPad, I can compile code with java.util.Scanner.nextInt() in it, however, when I try to run the code it error out every time.

The code is:

import java.util.Scanner;
public class ScannerApp
{
static Scanner sc = new Scanner(System.in);
public static void main(String[] args)
{
System.out.print("Enter an integer: ");
int x = sc.nextInt();
System.out.println("You entered " + x + ".");
}
}

The error message is:

Enter an integer: Exception in thread "main" java.util.NoSuchElementException
at java.util.Scanner.throwFor(Scanner.java:838)
at java.util.Scanner.next(Scanner.java:1461)
at java.util.Scanner.nextInt(Scanner.java:2091)
at java.util.Scanner.nextInt(Scanner.java:2050)
at ScannerApp.main(ScannerApp.java:11)

I can compile and run the above code from DOS prompt (command window).

TIA

Posted: Sat Dec 08, 2007 9:39 pm
by MudGuard
If you run the Java program from textpad, System.in is NOT connected to the correct source STDIN.

The only solution I know is NOT to run the Java program from within textpad.

Posted: Sun Dec 09, 2007 12:15 am
by mals@nash-inc.com
MudGuard:

That sucks!!! Are you saying I shouldn't run Java programs from TextPad?

I can run kinds of java programs from TextPad. Just that, at this moment, I can't run any type of a program that has java.util.Scanner.nextInt() in the code.

Posted: Sun Dec 09, 2007 8:32 am
by MudGuard
Those that expect input from STDIN.

Textpad is NOT a Java IDE. If you want a Java IDE, go to www.eclipse.org
Then you can not only run your programs from the IDE, you can even debug them and have many other Java-specific features (like code completion, instant error messages and so on)

Posted: Sun Dec 09, 2007 8:40 am
by mals@nash-inc.com
Okay, now I understand.
Thank you for taking the time.