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

Thread: Scanner closed error

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

    Default Scanner closed error

    What would be the problem with this code?
    import java.io.*;
    import java.util.Scanner; 
     
    /**
     * Write a description of class capital here.
     * 
     * @author (your name) 
     * @version (a version number or a date)
     */
    public class capital
    {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().trim().toLowerCase();
     
            // 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(); 
     
                // then the population info
                int population = countryInput.nextInt();
     
                // 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);
                }
                // be polite and close the file
                countryInput.close();
            }
        }
    }

    where I get this error:
    java.lang.IllegalStateException: Scanner closed
    at java.util.Scanner.ensureOpen(Scanner.java:1115)
    at java.util.Scanner.hasNext(Scanner.java:1379)
    at capital.main(capital.java:28)

    It doesnt seem to read the file, which was working before.
    And would anyone tell me how I can get a message to appear if there is no match from the file? If I use an else statement, the message appears on every line, causing the same message to appear 5 times.

    Basically the program is meant to read the file, user inputs part of the name, it shows user the information. If it cannot find it, it needs to show a helpful message.

    --- Update ---

    I really need help. Its due tomorrow, this is really stressful.


  2. #2
    Junior Member
    Join Date
    Apr 2013
    Posts
    25
    Thanks
    0
    Thanked 3 Times in 3 Posts

    Default Re: Scanner closed error

    Hi

    You need to put the statement countryInput.close(); i.e close the scanner after the while loop.
    And for displaying the message define a variable found before the while loop as int found = 0;
    increment it i.e found++ in the if condition.
    and outside the while loop i.e after putting countryInput.close() put the below condition
    if (found==0)
    {
    System.out.println("The File does not contain the input string");
    }
    hope this solves your problem.

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

    Default Re: Scanner closed error

    Sorry, I had the original file and now all i need is how to get a message displayed when there is no match in the file. I put something like this as a test:
     if(country.toLowerCase().contains(searchString))
                {
                    System.out.println(country + "\t" +
                        capital + "\t" + population);
                }
                else
                {
                    System.out.print("test");
                }

    and it resulted in this:
    test
    test
    test
    test
    test
    test
    test
    test
    test
    test
    test
    test
    test
    test
    test
    test
    test
    test
    test
    test
    test
    test
    test
    test
    test
    test
    test
    test
    test
    test
    test
    test
    test
    test
    test
    test
    test
    test
    test
    test
    test
    test
    test
    test
    test
    test
    test

    This would indicate it is going through the file and trying to find the phrase, then returning test with each line that doesnt have the phrase.
    So how can I get this to occur once?

  4. #4
    Junior Member
    Join Date
    Apr 2013
    Posts
    25
    Thanks
    0
    Thanked 3 Times in 3 Posts

    Default Re: Scanner closed error

    Hi

    You can define a counter variable "found" before the while loop as int found = 0;
    increment it i.e found++ in the if condition.
    and outside the while loop put the below condition
    if (found==0)
    {
    System.out.println("The File does not contain the input string");
    }

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

    Default Re: Scanner closed error

    How would this look in my code above?
    Because I have tried and it doesnt work. I might be doing something wrong.

  6. #6
    Junior Member
    Join Date
    Apr 2013
    Posts
    25
    Thanks
    0
    Thanked 3 Times in 3 Posts

    Default Re: Scanner closed error

    hi

    have appended the wid your code below

    public class capital
    {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().trim().toLowerCase();

    // open the data file
    File countryFile = new File("D:/Nut SMS/Project/SMSApplicationService/src/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
    int found = 0;
    while(countryInput.hasNext())
    {
    // read the 3 parts of the line

    // first the country and capital
    String country = countryInput.next();
    String capital = countryInput.next();

    // then the population info
    int population = countryInput.nextInt();

    // 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);
    found ++;

    }


    // be polite and close the file


    }
    if (found==0)
    {
    System.out.println("The File does not contain the input string");
    }
    countryInput.close();
    }
    }

  7. The Following User Says Thank You to hope_08 For This Useful Post:

    dx2731 (April 16th, 2013)

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

    Default Re: Scanner closed error

    Thankyou so much! Now I see what I did wrong xD
    The code works as it should

Similar Threads

  1. socket closed
    By tyler_long_sim in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 21st, 2013, 05:52 PM
  2. error with "Scanner" class in basic calculator program.
    By wheezebeez in forum What's Wrong With My Code?
    Replies: 4
    Last Post: November 2nd, 2012, 09:49 AM
  3. Resource leak: 'in' is never closed..?
    By missm in forum What's Wrong With My Code?
    Replies: 3
    Last Post: October 3rd, 2012, 02:01 PM
  4. Result set is closed
    By keshav_agrawal89 in forum What's Wrong With My Code?
    Replies: 6
    Last Post: June 11th, 2011, 08:35 AM
  5. ResultSet is Closed
    By ramayya4u in forum JavaServer Pages: JSP & JSTL
    Replies: 1
    Last Post: May 24th, 2009, 03:54 AM