Thread: Exception

    #1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2004
    Posts
    208
    Rep Power
    13

    Exception


    Hi,

    I got the Exception when I ran my code below. The compilation works fine. I don't know why it thows NoSuchElementException. Please help. Thanks.

    Code:
    import java.util.Scanner;
    
    public class StringTest
    {
       public static void main( String args[] )
       {
          Scanner scanner = new Scanner( System.in );
          System.out.print( "Please enter an integer: ");
          int num = scanner.nextInt();
       }
    }
    Please enter an integer: Exception in thread "main" java.util.NoSuchElementException
    at java.util.Scanner.throwFor(Scanner.java:907)
    at java.util.Scanner.next(Scanner.java:1530)
    at java.util.Scanner.nextInt(Scanner.java:2119)
    at StringTest.main(StringTest.java:17)

    Tool completed with exit code 1
  2. #2
  3. Lord of the Dance
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Oct 2003
    Posts
    3,647
    Rep Power
    1945
    Is that the complete code?

    The message says the error is at line 17, but there are only 11 lines in the posted code.
  4. #3
  5. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,711
    Rep Power
    348
    Read the API doc for the Scanner class's nextInt() method to see why it throws that exception.
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2004
    Posts
    208
    Rep Power
    13
    Thanks for your responses!

    No, it is not my complete code. I actually commented out several statememts that did not relate to the problem.

    After reading nextInt() of the Scanner class, I ran the code at the command prompt, the code worked fine. I only have the problem in Textpad. It seems like Textpad won't let me enter the input. Therefore, it threw the exception. I am not sure how to do it in Textpad.
  8. #5
  9. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,711
    Rep Power
    348
    Not all IDEs support a console that allows users to enter data to the java programs that they execute.
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2004
    Posts
    208
    Rep Power
    13
    I see. Thanks, NormR!

IMN logo majestic logo threadwatch logo seochat tools logo