Question

The text files boynames.txt and girlnames.txt, which are included in the source code for this boo...

The text files boynames.txt and girlnames.txt, which are included in the source code for this book, contain lists of the 1,000 most popular boy and girl names in the United States for the year 2005, as compiled by the Social Security Administration.

These are blank-delimited files where the most popular name is listed first, the second most popular name is listed second, and so on to the 1,000th most popular name, which is listed last. Each line consists of the first name followed by a blank

VideoNote

space followed by the number of registered births in the year using that name. For example, the girlnames.txt file begins with

   Emily 25494
   Emma 22532

This indicates that Emily is the most popular name with 25,494 registered nam- ings, Emma is the second most popular with 22,532, and so on.

Write a program that determines how many names are on both the boys’ and the girls’ list. Use the following algorithm:

  • Read each girl name as a String, ignoring the number of namings, and add it to a HashSet object.

  • Read each boy name as a String, ignoring the number of namings, and add it to the same HashSet object. If the name is already in the HashSet, then the add method returns false. If you count the number of false returns, then this gives you the number of common namings.

  • Add each common name to an ArrayList and output all of the common names from this list before the program exits.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashSet;

public class CommonNames {
   static HashSet<String> names = new HashSet<String>();
   static ArrayList<String> commonNames = new ArrayList<String>();

   public static void main(String... args) {

       readFile("girlnames.txt"); // read girls names file
       readFile("boynames.txt"); // read boys names file
       printCommanNames(); // print common names in both files

   }

   // Method to read names from file specified
   private static void readFile(String fileName) {

       try {
           FileInputStream fileStream = new FileInputStream(fileName); // file input stream object to read file
           DataInputStream dataInputStream = new DataInputStream(fileStream);
           BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(dataInputStream));
           String line;
           while ((line = bufferedReader.readLine()) != null) { // loop through file to read names
               String[] name = line.split(" "); // split by space to read name and count in array
               if (!names.add(name[0])) // if name is already present in hashset then add to commonNames arraylist
                   commonNames.add(name[0]);
           }
           dataInputStream.close(); // close input stream
       } catch (Exception e) {
           System.err.println("Error: " + e.getMessage()); // print error
       }
   }

   // Method to print common Names
   private static void printCommanNames() {

       System.out.println("Common Names are :");
       for (int i = 0; i < commonNames.size(); i++) { // loop through arraylist to print names

           System.out.println(commonNames.get(i));
       }

   }

}

//Output

Problems Javadoc DeclarationConsole terminated> CommonNames [Java ApplicationCPogram Files Javaljdk1.8.0 601bin javaw.exe(25-

Add a comment
Know the answer?
Add Answer to:
The text files boynames.txt and girlnames.txt, which are included in the source code for this boo...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Need help with the code for this assignment. Python Assignment: List and Tuples We will do...

    Need help with the code for this assignment. Python Assignment: List and Tuples We will do some basic data analysis on information stored in external files. GirNames.txt contains a list of the 200 most popular names given to girls born in US from year 2000 thru 2009: (copy link and create a txt file): https://docs.google.com/document/d/11YCVqVTrzqQgp2xJqyqPruGtlyxs2X3DFWn1YUb3ddw/edit?usp=sharing BoyNames.txt contains a list of the 200 most popular names given to boys born in US from year 2000 thru 2009 (copy link and create...

  • I need help with this question for programming. The language we use is C++ The program...

    I need help with this question for programming. The language we use is C++ The program should ask the user if they want to search for a boy or girl name. It should then ask for the name search the appropriate array for the name and display a message saying what ranking that name received, if any. The name files (BoyNames.txt and GirlNames.txt) are in order from most popular to least popular and each file contains two hundred names. The...

  • The code will not run and I get the following errors in Visual Studio. Please fix the errors. err...

    The code will not run and I get the following errors in Visual Studio. Please fix the errors. error C2079: 'inputFile' uses undefined class 'std::basic_ifstream<char,std::char_traits<char>>' cpp(32): error C2228: left of '.open' must have class/struct/union (32): note: type is 'int' ): error C2065: 'cout': undeclared identifier error C2065: 'cout': undeclared identifier error C2079: 'girlInputFile' uses undefined class 'std::basic_ifstream<char,std::char_traits<char>>' error C2440: 'initializing': cannot convert from 'const char [68]' to 'int' note: There is no context in which this conversion is possible error...

  • Write a program that reads in BabyNames.txt and produces two files, boynames.txt and girlnames.txt, separating the...

    Write a program that reads in BabyNames.txt and produces two files, boynames.txt and girlnames.txt, separating the data for the boys and the girls, and listing them in alphabetical order. This is the code I have, Its not sorting the boys/girls names in seperate files. It is creatinf two files, but has all the names. I also need to alphabetize the names. with an Array. sort import java.io.File; import java.io.FileNotFoundException; import java.io.PrintWriter; import java.util.Scanner; public class BabyNames{ public static void main(String[]...

  •                                           &nb

                                                                                            “Baby names and birth weights” Introduction Babies are weighed soon after birth and this birth weight is recorded as part of a baby’s medical record. A low birth weight indicates increased risk for complications and such babies are given specialized care until they gain weight. Public health agencies, such as the Centers for Disease Control and Prevention (CDC) in the United States, keep records of births and birth weights. In this project you will write principled object-oriented C++ code...

  • Create a simple Java class for a Month object with the following requirements:  This program...

    Create a simple Java class for a Month object with the following requirements:  This program will have a header block comment with your name, the course and section, as well as a brief description of what the class does.  All methods will have comments concerning their purpose, their inputs, and their outputs  One integer property: monthNumber (protected to only allow values 1-12). This is a numeric representation of the month (e.g. 1 represents January, 2 represents February,...

  • 12.8 GPA reports using files Prompt the user by "Enter name of input file: " for...

    12.8 GPA reports using files Prompt the user by "Enter name of input file: " for the name of an input file and open that file for reading. The two input files for the tests are already at the zyBooks site.They each have a list of student names with the course they have taken, of the form Jacobs, Anthony ECS10 4 B- ECS20 4 C+ ANS17 3 A ANS49H 2 D Wilson, Elaine ECS10 4 B+ ECS20 4 C+ ANS17...

  • Your assignment is to write a grade book for a teacher. The teacher has a text file, which includ...

    Your assignment is to write a grade book for a teacher. The teacher has a text file, which includes student's names, and students test grades. There are four test scores for each student. Here is an example of such a file: Count: 5 Sally 78.0 84.0 79.0 86.0 Rachel 68.0 76.0 87.0 76.0 Melba 87.0 78.0 98.0 88.0 Grace 76.0 67.0 89.0 0.0 Lisa 68.0 76.0 65.0 87.0 The first line of the file will indicate the number of students...

  • Write the Java code for the class WordCruncher. Include the following members:

    **IN JAVAAssignment 10.1 [95 points]The WordCruncher classWrite the Java code for the class WordCruncher. Include the following members:A default constructor that sets the instance variable 'word' to the string "default".A parameterized constructor that accepts one String object as a parameter and stores it in the instance variable. The String must consist only of letters: no whitespace, digits, or punctuation. If the String parameter does not consist only of letters, set the instance variable to "default" instead. (This restriction will make...

  • It is a C++ program by using inheritance and vectors My professor said that all the files should be separate. like File.cpp, File.h, Text.cpp,Text.h,Main.cpp I already have these files File.cpp #inclu...

    It is a C++ program by using inheritance and vectors My professor said that all the files should be separate. like File.cpp, File.h, Text.cpp,Text.h,Main.cpp I already have these files File.cpp #include "File.h" // Constructor of File that takes File name and type as arguments File::File(string type, string name) {    this->type = type;    this->name = name; } //returns the type. string File::getType() {    return type; } //returns the name of file. string File::getName() {    return name; } File.h #ifndef __FILE_H__ #define...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT