Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: java.util.NoSuchElementException: null (in java.util.Scanner) Need help please?

  1. #1
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,145
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: java.util.NoSuchElementException: null (in java.util.Scanner) Need help please?

    What line does the error happen on? Please post the full text of the error message.

    The NoSuchElementException error is from a Scanner method that tried to read and didn't find anything.


    One problem I see is that hasNext() is not called before every call to next()
    If you don't understand my answer, don't ignore it, ask a question.


  2. #2
    Junior Member
    Join Date
    Apr 2013
    Location
    The wonderful land of Oz
    Posts
    3
    My Mood
    Stressed
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: java.util.NoSuchElementException: null (in java.util.Scanner) Need help please?

    The error happens on line 35. The full error message is:

    java.util.NoSuchElementException
    at java.util.Scanner.throwFor(Scanner.java:907)
    at java.util.Scanner.next(Scanner.java:1416)
    at Capitals.main(Capitals.java:35)

    and then in my code editor it brings up

    java.util.NoSuchElementException:
    null (in java.util.Scanner)


    I had read that somewhere else, but the information is still being displayed so now I'm just confused.
    ---------

    does having a hasNext() before the brackets there on line 29, not solve that problem? I guess I just assumed it would cover everything in the brackets :S

  3. #3
    Junior Member
    Join Date
    Mar 2013
    Posts
    24
    My Mood
    Dead
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: java.util.NoSuchElementException: null (in java.util.Scanner) Need help please?

    Im doing the same assignment, same error

  4. #4
    Junior Member
    Join Date
    Apr 2013
    Location
    The wonderful land of Oz
    Posts
    3
    My Mood
    Stressed
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: java.util.NoSuchElementException: null (in java.util.Scanner) Need help please?

    Yeah I saw your post before and realised we must have been doing the same assignment. Im just so confused, i don't know how to fix it D:

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,145
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: java.util.NoSuchElementException: null (in java.util.Scanner) Need help please?

    The hasNext() method tests if there is one line available to be read. The code then tries to read 3 lines without testing if there are 2 more lines available after the one.

    The code should check if there is a line available by calling hasNext() before every call to next().
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. java.util.NoSuchElementException
    By xSedated in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 24th, 2013, 06:03 AM
  2. Replies: 11
    Last Post: August 30th, 2012, 02:30 PM
  3. java.lang.String cannot be cast to java.util.Hashtable
    By ashin12 in forum What's Wrong With My Code?
    Replies: 6
    Last Post: April 1st, 2012, 06:17 AM
  4. Replies: 3
    Last Post: April 26th, 2011, 02:51 AM
  5. Replies: 4
    Last Post: April 29th, 2009, 10:53 AM