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
Code :
GB
BG
BG
GG
GB
GB
GB
GB
BG
BG
Here's code of what I have so far:
Code :
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 + "%");
Re: Reading Text Files Assignment [Scanner Method]
Quote:
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.
Re: Reading Text Files Assignment [Scanner Method]
Quote:
Originally Posted by
Norm
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.
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?
Re: Reading Text Files Assignment [Scanner Method]
Quote:
Originally Posted by
Norm
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++.
Re: Reading Text Files Assignment [Scanner Method]
Quote:
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?
Re: Reading Text Files Assignment [Scanner Method]
Quote:
Originally Posted by
Norm
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.
Re: Reading Text Files Assignment [Scanner Method]
Quote:
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. ????
Quote:
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.
Re: Reading Text Files Assignment [Scanner Method]
Quote:
Originally Posted by
Norm
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.