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

Thread: If no search results found..display a message....but how? is the question.

  1. #1
    Junior Member
    Join Date
    Apr 2013
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default If no search results found..display a message....but how? is the question.

    Hey guys,

    First time poster so quick background on my project. I've been asked to make a program that accesses a file and outputs what you've searched for. The file contains a list of the worlds capitols and their population. If you enter "North" it will spit out anything with that word in it, obviously. Problem is if there is no search result it does nothing...I want it to be able to display a message saying "Sorry, no results were found" but don't know where to start apart from the fact I know it would be a boolean varibale and an if statement in the end..guidance would be greatly appreciated

    Code was written in Bluej
    Source:

    import java.util.Scanner;
    import java.io.File;
    import java.io.FileNotFoundException;
     
     
    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().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();
        }
    }
    __________________________________________________ __________________________________

    Thank you in advance


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

    Default Re: If no search results found..display a message....but how? is the question.

    be a boolean varibale and an if statement in the end.
    That should do it. What have you tried?
    Define the variable before the search loop
    set it inside the loop if any matches found
    after the loop, test it and print the message
    If you don't understand my answer, don't ignore it, ask a question.

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

    Default Re: If no search results found..display a message....but how? is the question.

    I tried doing this:

    if(country.toLowerCase().contains(searchString))
                {
                    System.out.println(country + "\t" +
                        capital + "\t" + population);
     
                }
                else
                {     System.out.println("Sorry, no results were found");
     
                }

    But then it just posts "Sorry, no results were found" regardless of what I put in. Also it posts repeatedly...I'm guessing its looping infinitely?

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

    Default Re: If no search results found..display a message....but how? is the question.

    That looks like the message is displayed inside the loop. It will display a message for every item that does not match.

    Did you see this pseudo code in my last post?
    Define the boolean variable before the search loop
    set its value inside the loop if any matches found
    after the loop, test its value and print the message
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Apr 2013
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: If no search results found..display a message....but how? is the question.

    How would I set it's value in the loop?? - That's what confuses me.

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

    Default Re: If no search results found..display a message....but how? is the question.

    How would I set it's value
    What kind of variable are you setting?
    Give it a value so that when the loop exits you will know if a match was found.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Apr 2013
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: If no search results found..display a message....but how? is the question.

    I've set:
    boolean resultFound

    I'm guessing the next step would be to put this inside the loop:

    resultFound= something

    and then at the end:

    if(!resultFound)
           {
                System.out.println("Sorry, no results were found.");
     
            }

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

    Default Re: If no search results found..display a message....but how? is the question.

    What happens when you compile and execute the code? Are there errors? Did it work as desired?
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Junior Member
    Join Date
    Apr 2013
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: If no search results found..display a message....but how? is the question.

    I haven't compiled yet..still trying to figure out how to work it into the loop haha

Similar Threads

  1. Why am I getting a <No Main Classes Found> error message?? Help please!!
    By Rick Sebastian in forum What's Wrong With My Code?
    Replies: 13
    Last Post: February 9th, 2013, 07:04 PM
  2. How do i display each loop results
    By Amshank24 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: January 14th, 2013, 07:43 AM
  3. How to display the results + repeat to the number entered by user?
    By TheBattousaixx in forum What's Wrong With My Code?
    Replies: 1
    Last Post: September 17th, 2011, 11:56 PM
  4. How to display error message box
    By jasonxman in forum What's Wrong With My Code?
    Replies: 11
    Last Post: August 21st, 2011, 02:47 PM
  5. How can i view search results by using servlet and jsp?
    By noFear in forum Java Theory & Questions
    Replies: 3
    Last Post: August 27th, 2010, 11:22 AM