Code with using util.Scanner.nextInt() complies but not run
Posted: Fri Dec 07, 2007 11:47 pm
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
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