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 6 of 6

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

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

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

    Hi guys
    I'm a first time user so I hope I'm doing this right :S

    So I'm doing a workshop assessment for my uni course and i have to finish a program that reads a file with a list of countries, their capital cities and the populations of those cities. I've got my code and it compiles, but every time i run the program and enter my search, it brings up the answers, but brings up this message: java.util.NoSuchElementException: null (in java.util.Scanner), and i have absolutely no idea how to fix it. It's due midnight tomorrow, and I've spent the better part of two days trying to fix it to no avail. Also, i was required to add an if statement to the original code to display if no results were found. I added an if-else statement so a message should pop up either way, but the messages don't appear..I'm not sure if this is somehow related to my bit of code that's not working? I'm only 7 weeks into my course and have never used Java before, so my level of understanding is really quite basic at the moment. Any help anyone could give would be truly fantastic as I am really at a loss right now.

    import java.util.Scanner;
    import java.io.*;
    /**
     * A program to show countries, capitals and populations
     * 
     * @author  --LC--
     * @version PP Module 6, Semester 1, 2013
     */
    public class Capitals
    {
        public static void main(String[] args)throws FileNotFoundException
        {
            // ask the user for the search string
            Scanner keyboard = new Scanner(System.in);
            System.out.print("Please enter part of the country name: ");
            String searchString = keyboard.next().toLowerCase();
            boolean resultFound = false;
     
            // open the data file
            File countryFile = new File("CountryData.csv");
     
            // create a scanner from the file
            Scanner countryInput = new Scanner(countryFile);
     
            // set up the scanner to use "," as the delimiter
            countryInput.useDelimiter("[\\r,]");
     
            // read one line of data at a time, processing each line
            while(countryInput.hasNext())
            {
                // read the 3 parts of the line
     
                // first the country and capital
                String country = countryInput.next();
                String capital = countryInput.next(); //<<THIS HERE is the line bluej says i'm getting the exception at.
     
                // then the population info
                String population = countryInput.next();
     
                // print out the info if the country name contains the input search string
                if(country.toLowerCase().contains(searchString))
                {
                    System.out.println(country + "\t" +
                        capital + "\t" + population);
                }
                resultFound = true;
            }
            // get program to display message if no results are found
            if(resultFound)
            {
                System.out.println("Here are your results!");
            }
            else
            {
                System.out.println("No results found, sorry!");
            }
            // be polite and close the file
            countryInput.close();
        }
    }


  2. #2
    Administrator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    24,848
    Thanks
    64
    Thanked 2,645 Times in 2,615 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()

  3. #3
    Junior Member
    Join Date
    Apr 2013
    Location
    The wonderful land of Oz
    Posts
    4
    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

  4. #4
    Junior Member
    Join Date
    Mar 2013
    Posts
    25
    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

  5. #5
    Junior Member
    Join Date
    Apr 2013
    Location
    The wonderful land of Oz
    Posts
    4
    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:

  6. #6
    Administrator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    24,848
    Thanks
    64
    Thanked 2,645 Times in 2,615 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().

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
  2. Replies: 11
    Last Post: August 30th, 2012, 14:30
  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
  4. Replies: 3
    Last Post: April 26th, 2011, 02:51
  5. Replies: 4
    Last Post: April 29th, 2009, 10:53