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

Using the Java SDK with TextPad

Moderators: AmigoJack, bbadmin, helios, Bob Hansen, MudGuard

Post Reply
mals@nash-inc.com
Posts: 3
Joined: Fri Dec 07, 2007 11:40 pm

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

Post 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
User avatar
MudGuard
Posts: 1295
Joined: Sun Mar 02, 2003 10:15 pm
Location: Munich, Germany
Contact:

Post 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.
mals@nash-inc.com
Posts: 3
Joined: Fri Dec 07, 2007 11:40 pm

Post 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.
User avatar
MudGuard
Posts: 1295
Joined: Sun Mar 02, 2003 10:15 pm
Location: Munich, Germany
Contact:

Post 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)
mals@nash-inc.com
Posts: 3
Joined: Fri Dec 07, 2007 11:40 pm

Post by mals@nash-inc.com »

Okay, now I understand.
Thank you for taking the time.
Post Reply