Humanity
2013-05-17 15:46:25 UTC
i. The “names.txt” file contains data about popular baby names over the last 80 years in the United States (http://www.ssa.gov/OACT/babynames). Every 10 years, the data gives the 1000 most popular boy names and girl names for children born in the US. The data can be summarized in the following format:
...
Sam 99 131 168 236 278 380 467 408 466
Samantha 0 0 0 0 272 107 26 5 7
Samara 0 0 0 0 0 0 0 0 886
Samir 0 0 0 0 0 0 920 0 798
...
Each line has a name followed by the rank of that name in 1920, 1930, …, 2000 (9 numbers). A rank of 1 was the most popular name for that decade, while a rank of 907 was not that popular. A 0 means that the name was out of the 1000 popular names for that decade.
ii. Your program will give an introduction and then prompt the user for a name. Then it will read through the data file searching for that name. The search should be case-insensitive, meaning that you should find the name even if the file and the user use capital letters in different places. As an extreme example, SaMueL would match sAMUel.
iii. If your program finds the name, it should print out the statistics for that name onto screen. You are to reproduce this format exactly:
** Popularity of a baby name since year 1920 **
name? ada
1920: 154
1930: 196
1940: 244
1950: 331
CS 210 W. Li Assignment 6
1960: 445
1970: 627
1980: 962
1990: 0
2000: 0
Then, you need to save the statistics into a result file for that baby name. For the example above, the result file should be named as “Ada.txt”, which should contain contents in the following format exactly:
Ada,
1920: 154,
1930: 196,
1940: 244,
1950: 331,
1960: 445,
1970: 627,
1980: 962,
1990: 0,
2000: 0
iv. If the name is not in the data file, your program should generate a short message onto screen indicating the name was not found. You are to reproduce this format exactly:
** Popularity of a baby name since year 1920 **
name? kumar
name not found.
HOW DO I CHECK IF NAME EXIST, AND CONTINUE ONLY IF THE NAME EXIST, BELOW ARE MY CODES:
public class BabyNames
{
public static void main(String[] args)
throws FileNotFoundException
{
Scanner input = new Scanner(new File("names.txt"));
Scanner console = new Scanner(System.in);
Boolean bool = false;
System.out.println("** Popularity of a baby name since year 1920 **");
System.out.print("name? ");
String given = console.next();
PrintStream out = new PrintStream(new File(given+".txt"));
out.println(given + ",");
results(given, out, input);
}
public static void results(String given, PrintStream out, Scanner input)
{
while (input.hasNextLine())
{
String name = input.nextLine();
Scanner lnScan = new Scanner(name);
String line = lnScan.next();
line.toString();
int x = -1;
while (line.equalsIgnoreCase(given) && x <= 7)
{
int pop = lnScan.nextInt();
x++;
int year = 1920 + (10 * x);
System.out.println(year + ": " + pop);
if (x<=7)
{
out.println(year + ": " + pop + ",");
}
else if (x <= 8)
{
out.println(2000 + ": " + pop);
}
}
}
}
}