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

Thread: Boolean help

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

    Default Boolean help

    I need to use a boolean variable to keep track of whether any countries were found, and then check this with an “if”
    statement at the end within 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();
     
        }
    }

    I am not looking for advice. Im looking for a solution because I am very stuck. I know if I put an else statement under this part of the code
    {
    System.out.println(country + "\t" +
    capital + "\t" + population);
    }
    it puts the text I want, but repeats it. So it kinda did what i want. Other than that, im completely stuck. So no asking me to check my code and stuff, just a solution in code form

    thankyou


  2. #2
    Junior Member
    Join Date
    Mar 2013
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Boolean help

    Check for the country first in the file, once it found, assign the values for country, capital and population from that line, then make the display out of the loop

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

    Default Re: Boolean help

    Can you post the program's output and add some comments to the post describing what is wrong with it and showing what you want it to look like.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Boolean help
    By 07.350 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 14th, 2013, 12:44 AM
  2. [SOLVED] Boolean or If?
    By 0ffConstantly in forum What's Wrong With My Code?
    Replies: 3
    Last Post: October 26th, 2012, 11:32 AM
  3. What does '^' do? Is this a boolean?
    By ColeTrain in forum Java Theory & Questions
    Replies: 2
    Last Post: October 19th, 2012, 02:58 PM
  4. Boolean method returning a boolean value
    By Deprogrammer in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 21st, 2010, 10:56 AM
  5. Some help with boolean
    By JPetroSS in forum Java Theory & Questions
    Replies: 3
    Last Post: November 2nd, 2010, 05:38 AM