Problem: The text files boynames.txt http://www.filedropper.com/boynames and gir
ID: 3787108 • Letter: P
Question
Problem:
The text files boynames.txt http://www.filedropper.com/boynames and girlnames.txt http://www.filedropper.com/girlnames , which are included in the
source code for this book text , contain a list of the 1,000 most popular boy and
girl names in the United States for the year 2003 as compiled by the Social Security
Administration.
These are blank-delimited files, where the most popular name is listed first, the
second most popular name is listed second, and so on, to the 1,000 th most popular
name, which is listed last. Each line consists of the first name followed by a blank
space and then the number of registered births using that name in the year. For
example, the girlnames.txt file begins with
Emily 25494
Emma 22532
Madison 19986
This indicates that Emily was the most popular name with 25,494 registered
namings, Emma was the second most popular with 22,532, and Madison was the
third most popular with 19,986.
Write a program that reads both the girl and boy files into memory using arrays.
Then, allow the user to input a name. The program should search through both
arrays. If there is a match, then it should output the popularity ranking and the
number of namings. The program should also indicate if there is no match.
For example, if the user enters the name “Justice,” then the program should output
Justice is ranked 456 in popularity among girls with 655 namings.
Justice is ranked 401 in popularity among boys with 653 namings.
If the user enters the name “Walter,” then the program should output
Walter is not ranked among the top 1000 girl names.
Walter is ranked 356 in popularity among boys with 775 namings.
Explanation / Answer
Please follow the code and comments for description :
CODE :
import java.io.BufferedReader; // required imports for the code
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Scanner;
public class BoyGirl { // class to run the code
public static void main(String[] args) throws FileNotFoundException, IOException { // driver method
Scanner sc = new Scanner(System.in); // scanner class to read the data
ArrayList<String> boysData = new ArrayList<>(); // array data of the files
ArrayList<String> girlsData = new ArrayList<>();
// read the data from the files to array data
try (BufferedReader br = new BufferedReader(new FileReader("boynames.txt"))) {
String line;
while ((line = br.readLine()) != null) {
boysData.add(line);
}
}
// read the data from the files to array data
try (BufferedReader br1 = new BufferedReader(new FileReader("girlnames.txt"))) {
String lineData;
while ((lineData = br1.readLine()) != null) {
girlsData.add(lineData);
}
}
// prompt to enter and search the data
System.out.print("Please Enter a Name to Search : ");
String name = sc.nextLine();
// process to iterate over the data and check for the name in the girldata list
boolean found = false;
Iterator<String> iter = girlsData.iterator();
String curItem = "";
String rank = "";
int pos = 0;
while (iter.hasNext() == true) {
pos = pos + 1;
curItem = (String) iter.next();
rank = curItem.substring(curItem.indexOf(" ") + 1);// get the rank
curItem = curItem.substring(0, curItem.indexOf(" "));
if (curItem.equalsIgnoreCase(name)) {
found = true;
break;
}
}
if (found == false) {
pos = 0;
}
if (pos != 0) {
System.out.println(name + " is ranked " + pos + " in popularity among girls with " + Integer.parseInt(rank) + " namings.");
} else {
System.out.println(name + " is not ranked among the top 1000 girl names.");
}
// process to iterate over the data and check for the name in the boydata list
boolean fnd = false;
Iterator<String> itr = boysData.iterator();
String curData = "";
String rnk = "";
int ps = 0;
while (itr.hasNext() == true) {
ps = ps + 1;
curData = (String) itr.next();
rnk = curData.substring(curData.indexOf(" ") + 1);
curData = curData.substring(0, curData.indexOf(" "));// get the rank
if (curData.equalsIgnoreCase(name)) {
fnd = true;
break;
}
}
if (fnd == false) {
ps = 0;
}
if (ps != 0) {
System.out.println(name + " is ranked " + ps + " in popularity among boys with " + Integer.parseInt(rnk) + " namings.");
} else {
System.out.println(name + " is not ranked among the top 1000 boy names.");
}
}
}
OUTPUT :
Please Enter a Name to Search : Tanner
Tanner is not ranked among the top 1000 girl names.
Tanner is ranked 120 in popularity among boys with 3354 namings.
Hope this is helpful.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.