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

Hello, I asked this question yesterday but, I forgot to mention the program shou

ID: 3809456 • Letter: H

Question

Hello,

I asked this question yesterday but, I forgot to mention the program should be a java program.

Write a Java program that reads the contents of the two files into two separate arrays. The user should be able to enter a name the application will display messages indicating whether the names were among the most popular.

1. GirlNames.txt and BoyNames.txt contain a list of the 200 most popular names given to girls/boys born in the United States for the years 2000 through 2009.

2. Your application should use an array to hold the names.

3. The program should continue interacting with the user indefinitely unless the user chooses to quit by entering "QUIT" (should be case insensitive).

4. The user should enter a single name and the program will search the name in both lists. The user SHOULD NOT specify whether the search is for girls name or boys name. The program is responsible for finding either or both and telling the user where it found it.

5. The program should display one of the following four results (examples):

* The name 'Annabelle' was not found in either list.

* The name 'Xavier' was found in popular boy names list (line 81).

* The name 'Amanda' was found in popular girl names list (line 63).

* The name 'Jordan' was found in both lists: boy names (line 38) and girl names (line 75).

6. The search should be case insensitive, but the resulting message should always display names in properly capitalized format (the first letter uppercase all others lowercase).

7. The files should only be opened/loaded and closed once per program session (at the beginning of the program execution). During the search the files should be closed.

8. If any or both of the files are missing, display a message to the user informing which file is missing or if both files are missing and exit the program.

9. Your program should contain properly defined and used methods. For example, your program should have a method that loads a file into an array. This method should take one parameter for the file name to be loaded and return an array of Strings. Another method should take a name and an array reference and perform a search and return results to the calling method.

10. Do not upload input text files.

View required output

Test Case 1

BoyNames.txt

http://textuploader.com/dra8y

GirlNames.txt

http://textuploader.com/dra8w

Standard Input                 Files in the same directory
  AnnabelleENTER  xavierENTER  AMANDAENTER  jOrdAnENTER  quit  

BoyNames.txt

http://textuploader.com/dra8y

GirlNames.txt

http://textuploader.com/dra8w

Explanation / Answer

PopularName.java

import java.io.FileReader;
import java.io.IOException;
import java.util.Scanner;

public class PopularName {
  
public String[] loadFileInArray(String filename) throws IOException
{
FileReader fileReader = new FileReader(filename);
Scanner sc = new Scanner(fileReader);
  
String [] names = new String[200];
int i = 0;
while(sc.hasNextLine())
{
names[i] = sc.nextLine();
i++;
}
sc.close();
fileReader.close();
return names;
}
  
public int searchName(String[] names, String name)
{
int result = -1;
  
for(int i = 0; i < names.length; i++)
{
if (name.equals(names[i].toLowerCase()))
{
result = i+1;
break;
}
}
  
return result;
}
  
public static void main(String[] args)
{
PopularName popularname = new PopularName();
  
String[] boysNames = new String[200];
boolean isBoyFileMissing = false;
try
{
boysNames = popularname.loadFileInArray("BoyNames.txt");
}
catch (IOException e) {
System.out.println("BoyNames.txt is missing");
isBoyFileMissing = true;
}
  
String[] girlsNames = new String[200];
boolean isGirlFileMissing = false;
try
{
girlsNames = popularname.loadFileInArray("GirlNames.txt");
}
catch (IOException e) {
System.out.println("GirlNames.txt is missing");
isGirlFileMissing = true;
}
  
if (isBoyFileMissing && isGirlFileMissing)
{
System.out.println("Exiting as both file are missing.");
System.exit(1);
}
  
Scanner sc = new Scanner(System.in);
System.out.flush();
while(true)
{
System.out.print("Enter a name to search or type QUIT to exit: ");
String name = sc.nextLine();
if (name.toLowerCase().equals("quit"))
{
break;
}
  
int nameInBoyFile = popularname.searchName(boysNames, name.toLowerCase());
int nameInGirlFile = popularname.searchName(girlsNames, name.toLowerCase());
name = name.substring(0, 1).toUpperCase() + name.substring(1).toLowerCase();
if ((nameInBoyFile == -1) && (nameInGirlFile == -1))
{
System.out.println("The name '" + name + "' was not found in either list.");
}
else
{
if (((nameInBoyFile != -1) && (nameInGirlFile != -1)))
{
System.out.println("The name '" + name + "' was found in both lists: boy names (line " +
nameInBoyFile + ") and girl names (line " + nameInGirlFile + ").");
}
else
{
if (nameInBoyFile != -1) {
System.out.println("The name '" + name + "' was found in popular boy names list (line " +
nameInBoyFile + ").");
}
  
if (nameInGirlFile != -1) {
System.out.println("The name '" + name + "' was found in popular girl names list (line " +
nameInGirlFile + ").");
}
}
}
}
sc.close();
}

}

Sample run

Enter a name to search or type QUIT to exit: Annabelle
The name 'Annabelle' was not found in either list.
Enter a name to search or type QUIT to exit: Xavier
The name 'Xavier' was found in popular boy names list (line 81).
Enter a name to search or type QUIT to exit: Amanda
The name 'Amanda' was found in popular girl names list (line 63).
Enter a name to search or type QUIT to exit: Jordan
The name 'Jordan' was found in both lists: boy names (line 38) and girl names (line 75).
Enter a name to search or type QUIT to exit: quit

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote