Write a java code for: Use the three attached files (babyNames2015.txt, babyName
ID: 3711000 • Letter: W
Question
Write a java code for:
Use the three attached files (babyNames2015.txt, babyNames2010.txt, and babyNames1880.txt) for your input data.
Have the program handle a file not found exception and output an error message to the user if either input file doesn't exist
Have the program prompt the user for the gender and first name. The prompt should explain to user what data the program expects.
Output the popularity of the name in 1880, 2010, and in 2015.
If the name isn't found in the top 1000, have the program output "not found" for that year
Calculate the increase or decrease in popularity from 1880 to 2010, from 1880 to 2015, and from 2010 to 2015, (assuming the name was in the top 1000 for those years).
Increase in popularity example #1: The male name "Noah" was ranked #130 in 1880, #7 in 2010, and #1 in 2015 for males. From 1880 to 2010, Noah increased in popularity by 123 (130-7). From 1880 to 2015, Noah increased in popularity by 129. From 2010 to 2015, Noah increased in popularity by 6.
Increase in popularity example #2: The female name "Harper" wasn't ranked in 1880, was #119 in 2010, and was ranked #10 in 2015. From 1880 to 2010, output "N/A" for the female name Harper (since this wasn't in the top 1000 for 1880). For 1880 to 2015, output "N/A" for the female name Harper (since this wasn't in the top 1000 for 1880). For 2010 to 2015, output 109 the female name Harper increased in popularity by 109 (from #119 to #10).
Text file "babyNames1880"
Textfile "babyNames2010"
Textfile" babyNames2015"
//Please Note that "this is not the complete list of the names in each year "I provided the top 20th names in each year since each text file is way too long.
Explanation / Answer
BabyNames.java
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Scanner;
public class BabyNames {
@SuppressWarnings("resource")
public static void main(String[] args) throws FileNotFoundException {
Scanner sc = new Scanner(System.in);
System.out.println("Please enter name:");
String name = sc.nextLine();
System.out.println("Please enter gender:");
String gender = sc.nextLine();
BufferedReader input1880 = null,
input2010 = null,
input2015 = null;
FileReader fr = null;
if ((new File("babyNames1880").exists()) ||
(new File("babyNames2010").exists()) ||
(new File("babyNames2015").exists()))
throw new FileNotFoundException("Input files not found!");
fr = new FileReader("babyNames1880.txt");
input1880 = new BufferedReader(fr);
fr = new FileReader("babyNames2010.txt");
input2010 = new BufferedReader(fr);
fr = new FileReader("babyNames2015.txt");
input2015 = new BufferedReader(fr);
int rank_1880 = getRank(input1880, name, gender);
int rank_2010 = getRank(input2010, name, gender);
int rank_2015 = getRank(input2015, name, gender);
if(rank_1880 == -1)
System.out.println("Not found in year 1880");
else
System.out.println("Popularity in year 1880: "+ rank_1880);
if(rank_2010 == -1)
System.out.println("Not found in year 2010");
else
System.out.println("Popularity in year 2010: "+ rank_2010);
if(rank_2015 == -1)
System.out.println("Not found in year 2015");
else
System.out.println("Popularity in year 2015: "+ rank_2015);
if (rank_1880 != -1 && rank_2010 != -1) {
System.out.println("Popularity increase: " + ( rank_1880 -rank_2010) + " from year 1880 to 2010");
}
else {
System.out.println("Popularity increase N/A from year 1880 to 2010");
}
if (rank_1880 != -1 && rank_2015 != -1) {
System.out.println("Popularity increase: " + (rank_1880 - rank_2015) + " from year 1880 to 2015");
}
else {
System.out.println("Popularity increase N/A from year 1880 to 2015");
}
if (rank_2010 != -1 && rank_2015 != -1) {
System.out.println("Popularity increase: " + (rank_2010 - rank_2015) + " from year 2010 to 2015");
}
else {
System.out.println("Popularity increase N/A from year 2010 to 2015");
}
}
private static int getRank(BufferedReader br, String name, String gender) {
String sCurrentLine="";
int linesRead = 1, index;
try {
while ((sCurrentLine = br.readLine()) != null && linesRead<=1000) {
++linesRead;
sCurrentLine = sCurrentLine.trim().replaceAll(" +", " ");
//System.out.println(sCurrentLine);
String rankAndNames[] = sCurrentLine.split(" ");
index = (gender.toLowerCase().equals("male")? 1 : 2);
if (rankAndNames[index].toLowerCase().equals(name)){
//System.out.println("x"+rankAndNames[0]+ "x");
return Integer.parseInt(rankAndNames[0]);
}
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return -1;
}
}
To execute, use any of the IDEs or from command promt execute the following commands:
>javac BabyNames.java
>java BabyNames
Note that the program assumes that the input files are in the same directory as of the BabyNames.java.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.