<p>OK, I HAVE BEEN TOILING WITH THIS PROGRAM FOR A COUPLE DAYS NOW AND CANNOT CRACK IT. our task was to write a prgm that creates two arraylists for boys and girls names. we then had to prompt the program to ask five families how many children they had, how many were boys and girls, and their names, and add all this to the arraylist. my prgm works, but I cant figure out how to add exceptions so it doesn't crash after a wrong input. I need to allow the user re-enter the correct answer if they ever enter a neg. num. of children, or if they enter string char. thanks guys. someone please help, I need it by tomm=), soory if the formatting is wired, I just copied and pasted....</p>
<p>int familynumber = 1; // first we create a counter to keep track of the number of families who have gone
Scanner in = new Scanner(System.in); // create scanner
ArrayList<string> bbn = new ArrayList<string>(); // , We create two array lists, one for boys and the other for girls, bbn is" boy baby names"
ArrayList<string> gbn = new ArrayList<string>(); // gbn is "girl baby names"</string></string></string></string></p>
<pre><code> do // use a do, while loop to execute the set of commands for a family five times for all of the users
{
System.out.println("Please enter the number of boys you have: "); // prompt the user to enter how many of their children are boys
int BOY = in.nextInt(); // store this value in variable "BOY"
String strb = Integer.toString(BOY); // we must convert this int value to a string in order to allow the correct number of name entries and because names are a string. store these names in the variable "strb"
int GIRL = NOC- BOY; // the remaining children in the family are of course female, store the number og girls in variable "GIRL"
String strg = Integer.toString(GIRL); // also convert this int value to string for correct number of name entries, store these names in var. "strg"
for(int i = 1; i<= BOY; i++) // create a for loop in order to create the correct number of name entry slots to the number of boys
{
System.out.println("Please enter your boys names: "); // prompt the user to enter their male names
String boysnames = in.next();
bbn.add(boysnames);
}
for(int i = 1; i<= GIRL; i++) // do the same for the girl name entries
{
System.out.println(" Now please enter your remaining girl's names: "); // prompt them to enter their names
String girlsnames = in.next();
gbn.add(girlsnames);
}
gbn.add(strg); // add the names to the appropriate arraylist
bbn.add(strb) ; // add the names to the appropriate arraylist
int sizeb = bbn.size(); // we must get the size of the boysnames arraylists after all the boy names have been added
int sizeg = gbn.size(); // we must get the size of the girlsnames arraylists after all the girls names have been added
familynumber++; // increment the counter so the loop doesn't go on forever
}
while(familynumber <= 5); // the while loop required after the do loop, runs the code for one family up to five times
System.out.println("The total number of boysnames for all of these families is " + (bbn.size() - 2)); // print out the total number of boys names
System.out.println("The total number of girlsnames for all of these families is " + (gbn.size() - 2)); // print out the total number of girls names
if(bbn.size() >= gbn.size()) // use an if - else statement to compare and print out the arraylist with the greater amount of names, meaning it is more popular
{
System.out.println("Therefore, out of all five families, the total number of boysnames is greater");
}
else
{
System.out.println("Therefore, out of all five families, the total number of girlsnames is greater");
}
}
}
</code></pre>