Help with Java homework Baby Name Ranking The popularity ranking of baby names f
ID: 3840761 • Letter: H
Question
Help with Java homework
Baby Name Ranking The popularity ranking of baby names from the years 2001 to 2010 is downloaded from www.ssa.gov/oact/babynames/ and stored in files named babynameranking2001.txt, babynameranking2002.txt, ..., babynameranking2010.txt. 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 babynameranking2010.txt are as follows: 1 Jacob 21,875 Isabella 22,731 2 Ethan 17,866 Sophia 20,477 So, the boy's name Jacob and girl's name Isabella are ranked #1 and the boy's name Ethan and girl's name Sophia ae ranked #2. There were 21,875 boys named Jacob and 22,731 girls named Isabella. Write a program that prompts the user to enter the year, gender, followed by name. The program will display the ranking of the name for that hear.
Here is a sample run:
Enter the year: 2010
Enter the gender: M
Enter the name: Javier
Javier is ranked #190 in the year 2010
Another sample run:
Enter the year: 2010
Enter the gender: F
Enter the name: ABC
The name ABC is not ranked in the year 2010
Explanation / Answer
#import java.io.*
#import java.util.Scanner;
class babyrank {
public static void main (String args[]){
String year;
String gender;
String name;
int found;
Scanner sc=new Scanner(System.in);
System.out.print("Enter the year:");
year = sc.next();
System.out.print("Enter the gender:");
gender = sc.next();
System.out.print("Enter the name:");
name = sc.next();
try {
String line;
BufferedReader br = new BufferedReader(new FileReader("babynameranking" + year + ".txt"));
count = 0; /* line count in the file */
found = 0 /* It is set when the name is found in the file
while ((line = br.readLine()) != null) {
count++;
if (line.contains(name){
found = 1;
System.out.println(name + " " + "is ranked " + count + "# in the year " + year);
break;
}
}
br.close();
if (found == 0){
System.out.println(name + " " + "is not ranked in the year " + year);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.