Please help me solve the problem(JAVA) the popularity ranking of baby names from
ID: 3720038 • Letter: P
Question
Please help me solve the problem(JAVA)
the popularity ranking of baby names from years 2001 to 2005 is downloaded from www.ssa.gov/oact/babynames and stored in files named babynameranking2001.txt, babynameranking2002.txt, …, babynameranking2005.txt. You can access these files by unzipping babynameraking.zip. Each file contains one thousand lines. Each line contains a ranking, a boy’s name, number for the boy’s name, a girl’s name, and number for the girl’s name. For example, the first two lines in the file babynameranking2001.txt are as follows:
So, the boy’s name Jacob and girl’s name Emily are ranked #1 and the boy’s name Michael and girl’s name Madison are ranked #2. 32,509 boys are named Jacob and 25,048 girls are named Emily. Write a program that prompts the user to enter the year, gender, and followed by a name, and displays the ranking of the name for the year. Here is a sample run:
Hint:
Create two two-dimensional arrays to store names:
String[][] boyNames = new String[5][1000];
String[][] girlNames = new String[5][1000];
boyNames[0][0] is the name rank #1 for year 2001.
boyNames[4][10] is the name rank #11 for year 2005.
girlNames[3][8] is the name rank #9 for year 2004.
girlNames[2][342] is the name rank #343 for year 2003.
And Write a program that prompts the user to enter one of the filenames described in the LAST Question and displays the names that are used for both genders in the file. Here is a sample run:
The popularity ranking of baby names from years 2001 to 2005 is downloaded from www.ssa.govlooct/babynames and stored in files named babynameranking2001.txt, babynameranking2002.txt,.. babynameranking2005.txt. You can access these files by unzipping babymameraking.zip. Each file contains one thousand lines. Each line contains a ranking, a boy's name, number for the boy's name, a girl's name, and number for the girl's name. For example, the first two lines in the file babynameranking2001.txt are as follows: acob Michael 32,509 29,666 Emily Madison 25,048 22,149 So, the boy's name Jacob and girl's name Emily are ranked #1 and the boy's name Michael and girl's name Madison are ranked #2 32,509 boys are named Jacob and 25048 girls are named Emily write a program that prompts the user to enter the year, gender, and followed by a name, and displays the ranking of the name for the year. Here is a sample run: Koutput> Enter the year: 2083 Enter the gender: M KOutput> Enter the year: 2004 Enter the gender: FExplanation / Answer
import java.io.*;
import java.util.*;
public class Demo154{
public static void main(String[] args){
String[][] boyNames = new String[5][1000];
String[][] girlNames = new String[5][1000];
Scanner sc = new Scanner(System.in);
int[] count = new int[5];
for (int i = 0; i<5; i++){
try {
System.out.print("Enter the file name for baby name ranking:");
String name = sc.nextLine();
FileInputStream fin = new FileInputStream(name);
Scanner fc = new Scanner(fin);
count[i] = 0;
while (fc.hasNextLine()){
String line = fc.nextLine();
String[] list = line.split(" ");
boyNames[i][count[i]] = list[1];
girlNames[i][count[i]] = list[3];
count[i]++;
}
System.out.println("There are " + Integer.toString(count[i]) + " names for both the genders.");
fc.close();
} catch (Exception e){
e.printStackTrace();
}
}
while(true){
System.out.println("Enter the year:");
int year = Integer.parseInt(sc.nextLine());
System.out.println("Enter the gender:");
String gender = sc.nextLine();
System.out.println("Enter the name:");
String name = sc.nextLine();
if (year == 2001){
if (gender.equalsIgnoreCase("M")){
int found = 0;
for (int i = 0; i<count[0]; i++){
if (boyNames[0][i].equalsIgnoreCase(name)){
System.out.println(name + " is ranked " + Integer.toString(i+1) + " in the year 2001.");
found = 1;
break;
}
}
if (found == 0){
System.out.println("The " + name + " is not ranked in the year 2001.");
}
}
if (gender.equalsIgnoreCase("F")){
int found = 0;
for (int i = 0; i<count[0]; i++){
if (girlNames[0][i].equalsIgnoreCase(name)){
System.out.println(name + " is ranked " + Integer.toString(i+1) + " in the year 2001.");
found = 1;
break;
}
}
if (found == 0){
System.out.println("The " + name + " is not ranked in the year 2001.");
}
}
}
if (year == 2002){
if (gender.equalsIgnoreCase("M")){
int found = 0;
for (int i = 0; i<count[0]; i++){
if (boyNames[1][i].equalsIgnoreCase(name)){
System.out.println(name + " is ranked " + Integer.toString(i+1) + " in the year 2002.");
found = 1;
break;
}
}
if (found == 0){
System.out.println("The " + name + " is not ranked in the year 2002.");
}
}
if (gender.equalsIgnoreCase("F")){
int found = 0;
for (int i = 0; i<count[0]; i++){
if (girlNames[1][i].equalsIgnoreCase(name)){
System.out.println(name + " is ranked " + Integer.toString(i+1) + " in the year 2002.");
found = 1;
break;
}
}
if (found == 0){
System.out.println("The " + name + " is not ranked in the year 2002.");
}
}
}
if (year == 2003){
if (gender.equalsIgnoreCase("M")){
int found = 0;
for (int i = 0; i<count[0]; i++){
if (boyNames[2][i].equalsIgnoreCase(name)){
System.out.println(name + " is ranked " + Integer.toString(i+1) + " in the year 2003.");
found = 1;
break;
}
}
if (found == 0){
System.out.println("The " + name + " is not ranked in the year 2003.");
}
}
if (gender.equalsIgnoreCase("F")){
int found = 0;
for (int i = 0; i<count[0]; i++){
if (girlNames[2][i].equalsIgnoreCase(name)){
System.out.println(name + " is ranked " + Integer.toString(i+1) + " in the year 2003.");
found = 1;
break;
}
}
if (found == 0){
System.out.println("The " + name + " is not ranked in the year 2003.");
}
}
}
if (year == 2004){
if (gender.equalsIgnoreCase("M")){
int found = 0;
for (int i = 0; i<count[0]; i++){
if (boyNames[3][i].equalsIgnoreCase(name)){
System.out.println(name + " is ranked " + Integer.toString(i+1) + " in the year 2004.");
found = 1;
break;
}
}
if (found == 0){
System.out.println("The " + name + " is not ranked in the year 2004.");
}
}
if (gender.equalsIgnoreCase("F")){
int found = 0;
for (int i = 0; i<count[0]; i++){
if (girlNames[3][i].equalsIgnoreCase(name)){
System.out.println(name + " is ranked " + Integer.toString(i+1) + " in the year 2004.");
found = 1;
break;
}
}
if (found == 0){
System.out.println("The " + name + " is not ranked in the year 2004.");
}
}
}
if (year == 2005){
if (gender.equalsIgnoreCase("M")){
int found = 0;
for (int i = 0; i<count[0]; i++){
if (boyNames[4][i].equalsIgnoreCase(name)){
System.out.println(name + " is ranked " + Integer.toString(i+1) + " in the year 2005.");
found = 1;
break;
}
}
if (found == 0){
System.out.println("The " + name + " is not ranked in the year 2005.");
}
}
if (gender.equalsIgnoreCase("F")){
int found = 0;
for (int i = 0; i<count[0]; i++){
if (girlNames[4][i].equalsIgnoreCase(name)){
System.out.println(name + " is ranked " + Integer.toString(i+1) + " in the year 2005.");
found = 1;
break;
}
}
if (found == 0){
System.out.println("The " + name + " is not ranked in the year 2005.");
}
}
}
}
}
}