Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Hi I need help on how to do the following Modify the “BabyNames.java” so it will

ID: 3859869 • Letter: H

Question

Hi I need help on how to do the following

Modify the “BabyNames.java” so it will do the following:

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.

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.

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

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

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.

In this case, you should not generate the result file.

AFTER you successfully find the name, print out to screen and save to the result file, try drawing a graph for the ranks of that name over time. One example is

Explanation / Answer

import java.util.*;
import java.awt.*;
import java.io.*;

public class BabyNames {

   // method to search the text file for inputted name
   public static String search(Scanner inputfile, String inputName) throws FileNotFoundException {
       PrintWriter writer = new PrintWriter("output.txt");          

       while(inputfile.hasNextLine()) {
           String listName = inputName.toLowerCase();
           String listline = inputfile.nextLine();
           String lowercaseLine = listline.toLowerCase();
          
           if(lowercaseLine.indexOf(listName) == 0 && lowercaseLine.charAt(listName.length()) == ' ') {
               int year = 1920;
               int start = listName.length();
               int end = start+1;


               while(end != lowercaseLine.length()){
                   if(lowercaseLine.charAt(end) == ' '){
                       String sub = lowercaseLine.substring(start,end);
                       String result = String.valueOf(year) + ": " + sub;
                       System.out.println(year + ": " + sub);
                       writer.println(result);
                       year = year + 10;
                       start = end;
                   }

                   end++;
               }
               String sub = lowercaseLine.substring(start,end);
               String result = String.valueOf(year) + ": " + sub;
               System.out.println(year + ": " + sub);
               writer.println(result);
               writer.close();
               return listline;
           }
       }
       return "";
   }


   //main method
   public static void main(String[] args) throws FileNotFoundException {
       System.out.println("** Popularity of a baby name since year 1920 **");
       System.out.println();
       System.out.print("Name? ");
       Scanner console = new Scanner(System.in);
       String name = (console.next());

       Scanner names = new Scanner(new File("babies.txt"));
       String found = search(names, name);
       if (found.length() == 0) {
           System.out.println("not found.");
       }
   }
  


}

/*
1)

** Popularity of a baby name since year 1920 **
output.txt
Name? samir
1920: 0
1930: 0
1940: 0
1950: 0
1960: 0
1970: 0
1980: 920
1990: 0
2000: 798

2)
** Popularity of a baby name since year 1920 **

Name? ayush
not found.


3)
** Popularity of a baby name since year 1920 **
output.txt
Name? sam
1920: 99
1930: 131
1940: 168
1950: 236
1960: 278
1970: 380
1980: 467
1990: 408
2000: 466