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: Reading Text Files Assignment [Scanner Method]

  1. #1
    Junior Member iAce's Avatar
    Join Date
    Dec 2012
    Location
    || ^_^ ||
    Posts
    19
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Reading Text Files Assignment [Scanner Method]

    I'm a beginner coder in Java and I'm confused about how to code this one assignment I have. If I can get any help, then it would be fantastic.

    I am using BlueJ.

    Assignment:
    Write a program to calculate the probability that a family with two children
    will consist of two boys, a boy and a girl, or two girls given a text file with combinations already given (BB, BG, GG). They want me to show correct use of loops and using the Scanner method to read text files.

    test1.txt
    GB
    BG
    BG
    GG
    GB
    GB
    GB
    GB
    BG
    BG

    Here's code of what I have so far:

        double sampleSize = 0.0;
        double twoBoysCounter = 0.0;
        double oneBoyGirlCounter = 0.0;
        double twoGirlsCounter = 0.0;
     
        Scanner inFile = new Scanner(new File("test1.txt"));
          while (inFile.hasNext())
          {
            String token = inFile.nextLine();
            String line = token;
            {          
                if (line.equalsIgnoreCase("BB"))
                {
                    twoBoysCounter++;
                }
                else if (line.equalsIgnoreCase("BG") || line.equalsIgnoreCase("GB"))
                {
                    oneBoyGirlCounter++;
                }
                else
                {
                    twoGirlsCounter++;
                }
            }
        }
        double twoBoys = twoBoysCounter / sampleSize;
        double oneBoyGirl = oneBoyGirlCounter / sampleSize;
        double twoGirls = twoGirlsCounter / sampleSize;
     
        System.out.println("Sample Size: " + sampleSize);
        System.out.println("Two Boys: " + twoBoys + "%");
        System.out.println("One Boy One Girl: " + oneBoyGirl + "%");
        System.out.println("Two Girls: " + twoGirls + "%");


  2. #2
    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: Reading Text Files Assignment [Scanner Method]

    error-infested code
    Start by cleaning up the errors. Post the full text of the error messages.

    Or another approach: throw out what you have coded and start fresh.
    Make a list of the steps the program needs to do in the order that the steps need to be done.
    This is part of designing the program. Post the list here so we can see what you think needs to be done.
    When the list of steps is agreed on, then try writing the code.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member iAce's Avatar
    Join Date
    Dec 2012
    Location
    || ^_^ ||
    Posts
    19
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Reading Text Files Assignment [Scanner Method]

    Quote Originally Posted by Norm View Post
    Start by cleaning up the errors. Post the full text of the error messages.

    Or another approach: throw out what you have coded and start fresh.
    Make a list of the steps the program needs to do in the order that the steps need to be done.
    This is part of designing the program. Post the list here so we can see what you think needs to be done.
    When the list of steps is agreed on, then try writing the code.
    Thanks for the reply. Usually the errors are just variables that I haven't declared yet. Here are the things I need to code this program correctly (I think).
    (1) Declare all variables.
    (2) Declare the text file to be read.
    (3) Correctly use Scanner methods.
    (4) I need the computer to read each line to see if it says BB, BG, GB, or GG. (The part I need the most help with. Idk how to do that.)
    (5) Count the sample size of how many combinations were looked at. (Don't know how to do this either.)
    (6) Calculate the probability of each combination occurring based on the sample size given in the text file.

    How would I go about this? I updated the code.

  4. #4
    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: Reading Text Files Assignment [Scanner Method]

    Now break step 3 down further. Its not detailed enough. One thing to consider when reading from a file is when there isn't any data left. Keep reading until no more data.

    In step 4 Where do you count the occurrences of each type of token read?
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member iAce's Avatar
    Join Date
    Dec 2012
    Location
    || ^_^ ||
    Posts
    19
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Reading Text Files Assignment [Scanner Method]

    Quote Originally Posted by Norm View Post
    Now break step 3 down further. Its not detailed enough. One thing to consider when reading from a file is when there isn't any data left. Keep reading until no more data.

    In step 4 Where do you count the occurrences of each type of token read?
    In Step 3, I know I'm using Scanner inFile = new Scanner(new File("test1.txt")); to read the text file while the while loop is going. It will terminate after the last line is read.

    My problem is that I don't know how it will actually assign a variable to each line to figure out if that line said BB, BG, GB, or GG.

    The occurrences will be counted in the while loop when it equals BB, BG, GB, or GG with the use of counter++.

  6. #6
    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: Reading Text Files Assignment [Scanner Method]

    how it will actually assign a variable to each line
    Not sure what "assign a variable to each line" means? The code in Post#1 uses the content of each line to add 1 to a separate counter determined by the contents of each line. That code looks reasonable.

    Where and how is the sample size computed?
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member iAce's Avatar
    Join Date
    Dec 2012
    Location
    || ^_^ ||
    Posts
    19
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Reading Text Files Assignment [Scanner Method]

    Quote Originally Posted by Norm View Post
    Not sure what "assign a variable to each line" means? The code in Post#1 uses the content of each line to add 1 to a separate counter determined by the contents of each line. That code looks reasonable.

    Where and how is the sample size computed?
    I meant like how can I make it so that each line can be read one at a time to determine if it is equal to BB, BG, GB, or GG. I don't know how to do that.

    And that's another problem I have. I don't know how to compute the sample size.

  8. #8
    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: Reading Text Files Assignment [Scanner Method]

    each line can be read one at a time to determine if it is equal to BB, BG, GB, or GG.
    The posted code does that now. ????

    how to compute the sample size.
    What is the sample size? Can you count how many times the loop goes around?
    Define a counter variable and add one to it each time the loop goes around.


    I'm done for tonight, back tomorrow.
    If you don't understand my answer, don't ignore it, ask a question.

  9. The Following User Says Thank You to Norm For This Useful Post:

    iAce (December 9th, 2012)

  10. #9
    Junior Member iAce's Avatar
    Join Date
    Dec 2012
    Location
    || ^_^ ||
    Posts
    19
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Reading Text Files Assignment [Scanner Method]

    Quote Originally Posted by Norm View Post
    The posted code does that now. ????


    What is the sample size? Can you count how many times the loop goes around?
    Define a counter variable and add one to it each time the loop goes around.


    I'm done for tonight, back tomorrow.
    Oh wow I'm mad that it does already. I didn't even notice. I added another counter variable and the code runs perfectly now. Thanks a lot bro. Much love.

Similar Threads

  1. Reading files and writing files
    By ProgrammerNewbie in forum What's Wrong With My Code?
    Replies: 5
    Last Post: December 3rd, 2012, 12:13 AM
  2. [SOLVED] Using Scanner to read in text files
    By PeskyToaster in forum File I/O & Other I/O Streams
    Replies: 0
    Last Post: April 16th, 2012, 04:57 PM
  3. How to use Scanner for files?
    By Shwetaa in forum Java Theory & Questions
    Replies: 1
    Last Post: September 9th, 2011, 07:53 PM
  4. Reading and Writing Text Files
    By kappasig84 in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: March 1st, 2010, 07:16 PM
  5. Reading many files using a scanner
    By jayjames90 in forum File I/O & Other I/O Streams
    Replies: 2
    Last Post: October 22nd, 2009, 04:35 PM