Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

girlnames.txt 1. The text files boynames.txt and girlnames.txt, which are includ

ID: 3602238 • Letter: G

Question

girlnames.txt

1. The text files boynames.txt and girlnames.txt, 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 nan second most popular name is listed second, and so on, to the 1,000th 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 e is listed first, the 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

NOte : First we have to create two txt files named boynames.txt and girlnames.txt and save them in the location where our java files is located.Otherwise the program cant able to read the files which results in file not found exception.

______________

ReadBoyGirlNames.java

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class ReadBoyGirlNames {

public static void main(String[] args) {

//Declaring variables
String name;
int girlrank = 0, girlpopularity = 0, boyrank = 0, boypopularity = 0;
int i = 0;
int flag1 = 0, flag2 = 0;
Scanner sc = null;

//Creating Arrays
String boynames[] = new String[1000];
String girlnames[] = new String[1000];
int boyRanks[] = new int[1000];
int girlRanks[] = new int[1000];


//Reading the data from the files and populating into arrays
try {
sc = new Scanner(new File("boynames.txt"));
while (sc.hasNext()) {
boynames[i] = sc.next();
boyRanks[i] = Integer.parseInt(sc.next());
i++;
}
sc.close();
i = 0;
sc = new Scanner(new File("girlnames.txt"));
while (sc.hasNext()) {
girlnames[i] = sc.next();
girlRanks[i] = Integer.parseInt(sc.next());
i++;
}
//closing the files
sc.close();
} catch (FileNotFoundException e) {

e.printStackTrace();
}


/*
* Creating an Scanner class object which is used to get the inputs
* entered by the user
*/
sc = new Scanner(System.in);

//Getting the input entered by the user
System.out.print("Enter the name :");
name = sc.next();

//Searching for the user entered name in girls and boy names arrays
for (i = 0; i < girlnames.length; i++) {
if (name.equalsIgnoreCase(boynames[i])) {
boyrank = i;
boypopularity = boyRanks[i];
flag1 = 1;
}
if (name.equalsIgnoreCase(girlnames[i])) {
girlrank = i;
girlpopularity = girlRanks[i];
flag2 = 1;
}

}

//Displaying the output
if (flag1 == 1) {
System.out.println(name + " is ranked " + girlrank + " in popularity among girls with " + girlpopularity + " namings.");
} else {
System.out.println(name + " is not ranked among the top 1000 girl names.");
}

if (flag2 == 1) {
System.out.println(name + " is ranked " + boyrank + " in popularity among boys with " + boypopularity + " namings.");
} else {
System.out.println(name + " is not ranked among the top 1000 boy names.");
}

}

}

________________

Output:

Enter the name :Justice
Justice is ranked 455 in popularity among girls with 655 namings.
Justice is ranked 400 in popularity among boys with 653 namings.

______________

Output#2:

Enter the name :Walter
Walter is not ranked among the top 1000 girl names.
Walter is ranked 355 in popularity among boys with 775 namings.

_____________Could you rate me well.Plz .Thank You