Java: Write a program that reads the contents of two files, BoyNames.txt, and Gi
ID: 3913089 • Letter: J
Question
Java:
Write a program that reads the contents of two files, BoyNames.txt, and GirlNames.txt into separate ArrayLists. These 2 files contain the most popular boy and girl names (respectively) for the years 2000 through 2009.
After loading the 2 ArrayLists, prompt the user to enter one or more names. When the user does this, for each name entered, if the name is found in one of the ArrayLists, display a message indicating that the name was one of the most popular for boys or girls (depending on the ArrayList where the name was found). If a name is not found in either ArrayList, display a message indicating that the name was not a favorite name for boys or girls.
Each line of output should include the name, and whether or not it was a favorite name.
Explanation / Answer
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;
//Program saved as FavouriteNames.java
public class FavouriteNames {
public static void main(String[] args) {
//create two array lists to store the names of boys and girls respectively
ArrayList<String> boyNames = new ArrayList<String>();
ArrayList<String> girlNames = new ArrayList<String>();
try{
//open the text file that contains boy names
BufferedReader br = new BufferedReader(new FileReader("D://BoyNames.txt"));
String name;
//read each name from the file and store it in the array list
while ((name = br.readLine()) != null){
boyNames.add(name);
}
br.close();
}
catch (IOException e){
System.out.println("'BoyNames.txt' cannot be opened");
}
try{
//open the text file that contains girl names
BufferedReader br = new BufferedReader(new FileReader("D://GirlNames.txt"));
String name;
//read each name from the file and store it in the array list
while ((name = br.readLine()) != null){
girlNames.add(name);
}
br.close();
}
catch (IOException e){
System.out.println("'GirlNames.txt' cannot be opened");
}
//create another array list to store the names entered by the user
ArrayList<String> findNames = new ArrayList<String>();
Scanner input = new Scanner(System.in);
//prompt the user to enter one or more names
System.out.println("Enter one or more names (q to quit): ");
boolean repeat = true;
while(repeat){
//add each of the names entered by the user to an array list
String n = input.nextLine();
if(n.equalsIgnoreCase("q"))
repeat = false;
else
findNames.add(n);
}
System.out.println();
//search the names in the boys array list
for(int i = 0; i < findNames.size(); i++){
boolean isFound = false;
//search each name in the array list of boys names
for(int j = 0; j < boyNames.size(); j++){
if(!isFound){
//if name is found print that name is found in boys list
if(findNames.get(i).equalsIgnoreCase(boyNames.get(j))){
System.out.println("" + findNames.get(i) + " is favourite name for boys.");
isFound = true;
}
}
}
//search each name in the array list of girls names
for(int k = 0; k < girlNames.size(); k++){
if(!isFound){
//if name is found print that name is found in girls list
if(findNames.get(i).equalsIgnoreCase(girlNames.get(k))){
System.out.println("" + findNames.get(i) + " is favourite name for girls.");
isFound = true;
}
}
}
//if name is not fount print appropriate message
if(!isFound)
System.out.println("" + findNames.get(i) + " is not favourite name for boys or girls.");
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.