Question:
Reading .txt files help (Java)?
Helen Keyes
2012-01-20 18:40:54 UTC
I have this assignment that I don't understand how to do.. could somebody help me please?
These are the instructions:
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.

I have a .txt file with the data and I can import it, I just don't know what to do from there.
THANKS!

ps here is the program I have so far:


import java.util.Scanner;
import java.io.File;
import java.io.IOException;
public class TextFileReader
{
public static void main(String[] args) throws IOException
{
String token = "";
File fileName = new File("MaleFemaleInFamily.txt");
Scanner inFile = new Scanner(fileName);
while (inFile.hasNext())
{
token = inFile.next( );
System.out.println (token);

Scanner in = new Scanner(System.in);

System.out.println("What is the size of the sample?" );
int samplesize = in.nextInt();

int twoBoys = samplesize/10;
int twoGirls = samplesize/10;
int oneBoneG = samplesize/10;

System.out.println("Sample Size: " + samplesize );
System.out.println("Two Boys: " + twoBoys);
System.out.println("One Boy One Girl: " + oneBoneG);
System.out.println("Two Girls: " + twoGirls);
}
inFile.close();
}//end of main method
}//end of class
Five answers:
McFate
2012-01-20 18:57:07 UTC
You've obviously taken a crack at the problem, so I'd like to help. But without knowing what the file is supposed to contain, or what you're supposed to do with the data, it's hard to answer your question.



As written, your program prompts the user for every token in the file, which seems odd. Is it really supposed to be doing that?



@M
2016-12-25 04:22:05 UTC
1
?
2012-01-20 21:27:59 UTC
Here is the correct code with furthure additions to suite the requirement of the question::-

import java.util.Scanner;

import java.io.File;

import java.io.FileReader;

import java.io.IOException;

public class Family

{

public static void main(String[] args) throws IOException

{



int twoBoys = 0,twoGirls = 0,boyAndGirl = 0,sampleSize = 0,counter=0;

float probabilityOfBoy=0,probabilityOfGirl=0,probabilityOfBoyAndGirl=0;



String token = "";

Scanner inFile = new Scanner(new FileReader("MaleFemaleInFamily.txt"));

while (inFile.hasNext())

{

token = inFile.next( );

System.out.println (token);





if (token.equals("BB"))

{

twoBoys++;

counter++;



}

else if (token.equals("GG"))

{

twoGirls++;

counter++;



}

else if (token.equals("BG"))

{

boyAndGirl ++;

counter++;

}

else if (token.equals("GB"))

{ boyAndGirl++;

counter++;

}

sampleSize=counter;

System.out.println("Sample Size: " + sampleSize );

System.out.println("Two Boys: " + twoBoys);

System.out.println("One Boy One Girl: " + boyAndGirl);

System.out.println("Two Girls: " + twoGirls);

}

inFile.close();

probabilityOfBoy = (float)twoBoys / (float)sampleSize;

probabilityOfGirl = (float)twoGirls / (float)sampleSize;

probabilityOfBoyAndGirl = (float)boyAndGirl / (float)sampleSize;

System.out.println("Two Boys: "+ probabilityOfBoy);

System.out.println("One Boy One Girl: "+ probabilityOfBoyAndGirl);

System.out.println("Two Girls: " + probabilityOfGirl);



}//end of main method



}//end of class



//Note:-content of MaleFemaleInFamily.txt (sampled data)

BB

BB

BB

BB

GG

GG

GG

GG

GG

BG

BG

BG

BG

BG

BG

GB

GB

GB





Best of Luck ;-)
?
2016-04-27 17:44:44 UTC
If you should be looking a successful way to help your child learn to learn while avoiding any potential traps in the process then this program https://tr.im/OCUVS is everything you need.

Children Learning Reading program is separate into two components which are generally provided in separate eBook s. Having ordered the course you are given immediate use of each of the eBook s which means you can use the plan nearly quickly after spending money on it.

Children Learning Reading is user friendly for you personally and for your child, with small lesson and games this system is ideal to instruct how to see for a tiny child.
trey
2015-11-04 13:47:35 UTC
going off of D Buggers program, i made one that will work with BlueJ IDE perfectly. For some reason, when i moved his program into my IDE it didn t quite work right.



import java.util.Scanner;

import java.io.File;

import java.io.FileReader;

import java.io.IOException;

public class Family

{

public static void main(String[] args) throws IOException

{

int twoBoys = 0;

int twoGirls = 0;

int boyAndGirl = 0;

int sampleSize = 0;

int counter = 0;

int probabilityOfBoy = 0;

int probabilityOfGirl = 0;

int probabilityOfBoyAndGirl = 0;



String token = "";

Scanner inFile = new Scanner(new FileReader("test1.txt"));

while (inFile.hasNext())

{

token = inFile.next( );

System.out.println(token);

if (token.equalsIgnoreCase("BB"))

{

twoBoys++;

counter++;

}

else if (token.equalsIgnoreCase("GG"))

{

twoGirls++;

counter++;

}

else if (token.equalsIgnoreCase("BG"))

{

boyAndGirl++;

counter++;

}

else if (token.equalsIgnoreCase("GB"))

{ boyAndGirl++;

counter++;

}

sampleSize = counter;

}

inFile.close();

probabilityOfBoy = ((twoBoys*100) / sampleSize);

probabilityOfGirl = ((twoGirls*100) / sampleSize);

probabilityOfBoyAndGirl = ((boyAndGirl*100) / sampleSize);

System.out.println("Sample Size: "+ sampleSize);

System.out.println("Two Boys: "+ probabilityOfBoy+"%");

System.out.println("One Boy One Girl: "+ probabilityOfBoyAndGirl+"%");

System.out.println("Two Girls: " + probabilityOfGirl+"%");





}//end of main method

}//end of class


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...