The text file babynames.txt, contains a list of the 1000 most popular boy and gi
ID: 3571527 • Letter: T
Question
The text file babynames.txt, contains a list of the 1000 most popular boy and girl names for the year 2004. This is a space delimited file of 1000 entries in which the rank is listed first, followed by the corresponding boy name and girl name. The most popular names are listed first and the least popular names are listed last. For example, the file begins with
1 Jacob Emily
2 Michael Emma
3 Joshua Madison
4 Matthew Olivia
5 Ethan Hannah
6 Andrew Abigail
7 Daniel Isabella
8 William Ashley
9 Joseph Samantha
10 Christopher Elizabeth
11 Anthony Alexis
12 Ryan Sarah
13 Nicholas Grace
14 David Alyssa
15 Alexander Sophia
16 Tyler Lauren
This indicates that Jacob is the most popular boy name and Emily is the most popular girl name. Michael is the second most popular boy name and Emma is the second most popular girl name.
Write a program that allows the user to input a name. The program should then search for a matching name among the girls and boys. If a match is found, it should output the rank of the name. 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 406 in popularity among boys.
Justice is ranked 497 in popularity among girls.
If the user enters the name “Walter”, then the program should output:
Walter is ranked 366 in popularity among boys.
Walter is not ranked among the top 1000 girl names.
my problem is the teacher want me to do,
when i put Justice or JUStic or jUsTice , the result is same.
Justice is ranked 406 in popularity among boys.
Justice is ranked 497 in popularity among girls.
Explanation / Answer
Please follow the code and comments for description :
CODE :
import java.io.BufferedReader; // required imports for the class
import java.io.FileReader;
import java.io.IOException;
import java.util.Scanner;
public class NewClass { // class to run the code
public static void main(String[] args) { // driver method
BufferedReader br = null; // local variables
FileReader fr = null;
Scanner sc = new Scanner(System.in); // scanner class to run the code
try {
fr = new FileReader("input.txt"); // file reader class to read the data
br = new BufferedReader(fr);
String sCurrentLine;
br = new BufferedReader(new FileReader("input.txt")); // bufferred reader object
boolean bFound = false;
boolean gFound = false;
System.out.print("Please Enter the Name : "); // prompt
String name = sc.nextLine(); // read the data
System.out.println("");
while ((sCurrentLine = br.readLine()) != null) { // iterate over the data
String arr[] = sCurrentLine.split("\s"); // split over the data based on delimiter
int rank = Integer.parseInt(arr[0]); // read the data separately
String bName = arr[1];
String gName = arr[2];
if (name.equalsIgnoreCase(bName)) { // check for the names
bFound = true;
System.out.println(name + " is ranked " + rank + " in popularity among boys."); // message
}
if (name.equalsIgnoreCase(gName)) { // check for the data
gFound = true;
System.out.println(name + " is ranked " + rank + " in popularity among girls."); // message
}
}
if (bFound == false) { // if not found
System.out.println(name + " is not ranked among the top 1000 boys names."); // print to console
}
if (gFound == false) {
System.out.println(name + " is not ranked among the top 1000 girls names.");
}
} catch (IOException e) { // catch any exceptions
e.printStackTrace(); // print the data to console for exceptions
} finally {
try {
if (br != null) { // close the reader objects
br.close();
}
if (fr != null) {
fr.close();
}
} catch (IOException ex) {
ex.printStackTrace(); // print the data to console for exceptions
}
}
}
}
Below is the sample data and needs to be replaced with the original data.
babynames.txt :
1 Jacob Emily
2 Michael Emma
3 Joshua Madison
4 Matthew Olivia
5 Ethan Hannah
6 Andrew Abigail
7 Daniel Isabella
8 William Ashley
9 Joseph Samantha
10 Christopher Elizabeth
11 Anthony Alexis
12 Ryan Sarah
13 Nicholas Grace
14 David Alyssa
15 Alexander Sophia
16 Tyler Lauren
17 Ashley Ryan
OUTPUT :
run 1 :
Please Enter the Name : david
david is ranked 14 in popularity among boys.
david is not ranked among the top 1000 girls names.
run 2 :
Please Enter the Name : ryan
ryan is ranked 12 in popularity among boys.
ryan is ranked 17 in popularity among girls.
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.