This is the practice problem I'm trying to complete: Java Assignment
The two text files boynames.txt and girlnames.txt contain lists of the most popular boy and girl names in the US for the year 2005, as compiled by the Social Security Administration.
Each file has multiple records, one for each name and the number of children given that name. For each record the name comes first, followed by a blank space, followed by the number of registered births in the year using that name. Eg, Emily 25494 Robert 16743 Write a program that determines how many common names are on the boy's and girl's lists. Use the following steps: • Read each girl name as a String, ignoring the number of namings, and add it to a HashSet. • Read each boy name as a String, ignoring the number of namings, and add it to the same HashSet. If the name is already in the HashSet, then the add() method returns false which denotes a name common to boys and girls. • Add each common name to an ArrayList and output the total number of common names and each common name to the console.
And this is the code I already have:
package names;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashSet;
public class CommonNames {
static ArrayList<String> commonNamesArray = new ArrayList<String>();
static HashSet<String> nameHash = new HashSet<String>();
int boyCounter;
int girlCounter;
File fileBoyNames = new File("C:/CS280/boynames.txt");
File fileGirlNames = new File("C:/CS280/girlnames.txt");
public static void main(String[] args) {
readFile("girlnames.txt"); // read girls names file
readBoysNames("boynames.txt"); // read boys names file
printCommanNames(); // print common names in both files
}
// Method to read names from file specified
private static void readBoysNames(String fileBoyName) {
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 (!nameHash.add(name[0])) // if name is already present in hashset then add to commonNames arraylist
commonNamesArray.add(name[0]);
boyCounter++; //Cannotmake a static reference to the non-static field boyCounter
}
dataInputStream.close(); // close input stream
} catch (Exception e) {
System.out.println("Error: " + e.getMessage()); // print error
}
}
private static void readGirlsNames(String fileBoyName) {
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 (!nameHash.add(name[0])) // if name is already present in hashset then add to commonNames arraylist
commonNamesArray.add(name[0]);
girlCounter++; //Cannotmake a static reference to the non-static field boyCounter
}
dataInputStream.close(); // close input stream
} catch (Exception e) {
System.out.println("Error: " + e.getMessage()); // print error
}
}
// Method to print common Names
private static void printCommonNames() {
System.out.println("Common Names are :");
for (int i = 0; i < commonNamesArray.size(); i++) { // loop through arraylist to print names
System.out.println(commonNamesArray.get(i));
}
}
}
Can anyone please help? I'm confused on how to get the output they're asking for
A couple of things
public class CommonNames {
static ArrayList<String> commonNamesArray = new ArrayList<String>();
static HashSet<String> nameHash = new HashSet<String>();
You DO NOT need and SHOULD NOT be using this array here. All of your names should be added to nameHash
Also, the variables do not need to be static, and it is better to declare the variable types using just the interface, like
static List<String> commonNamesArray = new ArrayList<String>();
static Set<String> nameHash = new HashSet<String>
Again, no need to static:
private static void readBoysNames(String fileBoyName) {
private static void readGirlsNames(String fileBoyName) {
You only need one method with a file name parameter. You can load the names from both files in two different calls.
If you can load the names from both files into the set, you just need to iterate over it to list the common names
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.