Files: babynameranking2001.txt babynameranking2002.txt ………….. babynameranking201
ID: 3724808 • Letter: F
Question
Files:
babynameranking2001.txt
babynameranking2002.txt
…………..
babynameranking2010.txt
The popularity ranking of baby names from years 2001 to 2010 are stored in these files.
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:
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 are ranked #2. 21,875 boys are named Jacob and 22,731 girls are named Isabella.
A):Baby name popularity ranking
Write a java program that prompt the user to enter year, gender, and name and display the ranking for the name. Prompt the user to enter another inquiry or exit the program.(Add additional note to explain what is happened in the program)
Here is a sample run:
B) Name for both genders
Write ajava program that prompts the user to enter one of the filenames and displays the names that are used for both genders in the file. Use sets to store names and find common names in two sets.(Add additional note to explain what is happened in the program)
Here is a sample run:
Explanation / Answer
PE01.java
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
/**
* 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.
*/
public class PE01
{
public static void main(String args[])
{
// Create a new Scanner object
Scanner input = new Scanner(System.in);
// Prompt the user to enter year, gender, and name
System.out.println("Enter a year from 2001 to 2010: ");
int year = input.nextInt();
System.out.println("Enter the gender (F or M): ");
String gender = input.next();
System.out.println("Enter the name: ");
String name = input.next();
input.close();
File file = new File("babynameranking" + year + ".txt");
if (!file.exists())
{
System.out.println("No record for " + year);
System.exit(1);
}
int rank = 0;
try
{
Scanner read = new Scanner(file);
while (read.hasNext())
{
String s = read.nextLine();
String[] temp = s.split(" ");
if (gender.equalsIgnoreCase("M") && temp[1].contains(name))
{
rank = new Integer(temp[0]);
}
else if (temp[3].contains(name))
{
rank = new Integer(temp[0]);
}
}
}
catch (FileNotFoundException e)
{
e.printStackTrace();
}
if (rank == 0)
{
System.out.println("The name "+name+" is not ranked in year " + year);
File file2 = new File("babynamesranking" + year + ".txt");
try
{
Scanner read = new Scanner(file2);
while (read.hasNext())
{
String line = read.nextLine();
String lineArr[] = line.split(" ");
if (gender == "M")
{
if (lineArr[1].equalsIgnoreCase(String.valueOf(name.charAt(0))))
{
lineArr[0] = "hi";
}
}
if (gender == "F")
{
if (lineArr[3].equalsIgnoreCase(String.valueOf(name.charAt(0))))
{
for(int i = 0; i < 3; i++)
{
lineArr[i] = "hi";
}
}
}
System.out.println("3 highest ranked names in year " +
year + " that start with letter "+ name.charAt(0) +" are: ");
}
read.close();
}
catch (FileNotFoundException e)
{
e.printStackTrace();
}
}
else
{
System.out.println(name+" is ranked #"+rank+" in year "+ year);
}
}
}
Output:
Enter a year from 2001 to 2010:
2010
Enter the gender (F or M):
M
Enter the name:
Javier
No record for 2010
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.