Results 1 to 8 of 8
  1. #1
    plm-pusik is offline Member
    Join Date
    Aug 2010
    Posts
    17
    Rep Power
    0

    Default Scanner error message

    Hey, I was making a program that uses the scanner class but I got a weird error. I narrowed the code down and I found the source, but I don't know how to fix it. I get the error when I try to run the program, compiling it gives me no error. My code:
    Java Code:
    import java.util.Scanner;
    public class UserInput {
    	public static void main(String[] args) {
    		Scanner myInput = new Scanner(System.in);
    		int myNumber;
    		myNumber = myInput.nextInt();
    	}
    }
    Error message:
    Java Code:
    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 UserInput.main(UserInput.java:6)
    
    Tool completed with exit code 1
    I am using TextPad 5.3.1 and this development kit:
    Java SE Downloads - Sun Developer Network (SDN)


    I'm new to Java so please try to keep the answers well explained.
    Thanks for the help.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by plm-pusik View Post
    Hey, I was making a program that uses the scanner class but I got a weird error. I narrowed the code down and I found the source, but I don't know how to fix it. I get the error when I try to run the program, compiling it gives me no error. My code:
    Java Code:
    import java.util.Scanner;
    public class UserInput {
    	public static void main(String[] args) {
    		Scanner myInput = new Scanner(System.in);
    		int myNumber;
    		myNumber = myInput.nextInt();
    	}
    }
    Error message:
    Java Code:
    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 UserInput.main(UserInput.java:6)
    
    Tool completed with exit code 1
    I get no such error when I compile and run your program.

    I'm new to Java so please try to keep the answers well explained.
    Thanks for the help.
    All the volunteers here strive to give the best answers that they can. If something isn't clear, then just ask.

    Best of luck.

  3. #3
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,431
    Blog Entries
    7
    Rep Power
    20

    Default

    There must've been a non-digit (or not a minus sign) in the input buffer; the OP must've accidentally typed something.

    kind regards,

    Jos

  4. #4
    plm-pusik is offline Member
    Join Date
    Aug 2010
    Posts
    17
    Rep Power
    0

    Default

    Quote Originally Posted by JosAH View Post
    There must've been a non-digit (or not a minus sign) in the input buffer; the OP must've accidentally typed something.

    kind regards,

    Jos
    OP?

    When I try to run the program I don't even get to input anything ... it crashes before that.

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,307
    Rep Power
    25

    Default

    What OS are you on?
    How are you executing the program? From command prompt or from inside an IDE?

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,431
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by plm-pusik View Post
    OP?
    It's an acronym: OP == Original Poster.

    kind regards,

    Jos

  7. #7
    plm-pusik is offline Member
    Join Date
    Aug 2010
    Posts
    17
    Rep Power
    0

    Default

    Quote Originally Posted by Norm View Post
    What OS are you on?
    How are you executing the program? From command prompt or from inside an IDE?
    I'm on Windows Vista Home Premium SP 2 (unfortunately).
    I'm executing the program from an IDE (TextPad 5.3.1), using java.exe.

    Though, I think I fixed it ... it looks like I didn't setup TextPad properly. I tried a few things but it wouldn't work so I changed to Eclipse and it now works fine. I did not reinstall JDK so I'm guessing that wasn't the problem. It would still be nice to know what the error message means though, so that I know what I'm dealing with if I get it in the future. Any ideas?
    Last edited by plm-pusik; 08-28-2010 at 07:52 PM.

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,307
    Rep Power
    25

    Default

    know what the error message means
    Have you looked at the API doc for the method you are using?

    Here's a simple program that gets the error:
    Java Code:
          String input = "1 3";
          Scanner sc = new Scanner(input);
          while(true) {
            int x = sc.nextInt();
            System.out.println("x=" + x);
          }
    /*
    x=1
    x=3
    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 TestCode1.main(TestCode1.java:35)
    */
    Last edited by Norm; 08-28-2010 at 08:51 PM.

Similar Threads

  1. Error Message ..
    By Hamodi18 in forum New To Java
    Replies: 15
    Last Post: 07-11-2010, 03:31 AM
  2. Error Message????
    By Cubba27 in forum New To Java
    Replies: 11
    Last Post: 11-21-2009, 02:46 PM
  3. Error Message in JBuilder
    By RavenNevarmore in forum New To Java
    Replies: 4
    Last Post: 10-08-2008, 06:53 AM
  4. java error message
    By baileyr in forum New To Java
    Replies: 2
    Last Post: 01-23-2008, 03:47 AM
  5. error message on jsp
    By sandor in forum Web Frameworks
    Replies: 1
    Last Post: 04-11-2007, 02:10 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •