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

For JAVA ECLIPSE Write a program that reads the contents of the two files into t

ID: 3702635 • Letter: F

Question

For JAVA ECLIPSE

Write a 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.

Should look like test case in the bottom:

required output

Test Case 1

BoyNames.txt

GirlNames.txt

Pleas help.

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

BoyNames.txt

GirlNames.txt

Explanation / Answer

Answer :

package package1;

import java.io.BufferedReader;

import java.io.File;

import java.io.FileReader;

import java.util.Scanner;

public class Popular {

public static void main(String[] args) {

//Declare two string arrays to hold girls name and boys name

String[] girlsName,boysName;

//Create object of scanner class to take input from user.

Scanner scan=new Scanner(System.in);

//Variable name to hold name to be found

String name;

//Variable to hold index on which name was found in girls and boys list

int nameFoundinGirls,nameFoundInBoys;

//Create object of file class to check if file exists

File file1=new File("GirlNames.txt");

File file2=new File("BoyNames.txt");

//Check if both file exists or not.

if(!file1.exists() && !file2.exists()){

System.out.println("Both the files are missing");

}

else if(!file1.exists()){

System.out.println("GirlName.txt is missing");

}else if(!file2.exists()){

System.out.println("BoyNames.txt is missing");

}else{

//If both files are not missing, call loadFile method tomload data from file into arrays

girlsName=loadFile("GirlNames.txt");

boysName=loadFile("BoyNames.txt");

//Iterate the loop will user enter quit

while(true){

System.out.println(" Enter name to search or 'QUIT' if wants to exit: ");

name=scan.nextLine();

//Format name so that the first letter is uppercase and all others are lowercase

String formatName=name.substring(0, 1).toUpperCase()+name.substring(1,name.length()).toLowerCase();

//Check if enter quit, if yes, terminate the loop

if(name.equalsIgnoreCase("QUIT")){

break;

}else{

//Call function findName to check if name found.

//nameFoundinGirls and nameFoundInBoys represents the index where the name was found. If it's -1 means the name is not found.

nameFoundinGirls=findName(name.toLowerCase(),girlsName);

nameFoundInBoys=findName(name.toLowerCase(),boysName);

//Now check following conditions to see, if name is present in both list or girls list or boys list or nowhere.

//Print line nameFoundInBoys+1 or nameFoundInGirls+1 because array starts from index 0

if(nameFoundinGirls!=-1 && nameFoundInBoys!=-1){

System.out.println("The name "

+"'"+formatName+"'"

+" was found in both lists: boy names (line "+(nameFoundInBoys+1)+")"

+" and girl names (line"+(nameFoundinGirls+1)+")");

}

else if(nameFoundinGirls!=-1){

System.out.println("The name "

+"'"+formatName+"'"

+" was found in popular girls names list (line "+(nameFoundinGirls+1)+")");

}

else if(nameFoundInBoys!=-1){

System.out.println("The name "

+"'"+formatName+"'"

+" was found in popular boys names list (line "+(nameFoundInBoys+1)+")");

}else{

System.out.println("The name "

+"'"+formatName+"'"

+" was not found in either list.");

}

}

}

}

}

//Method to find name

private static int findName(String name, String[] names) {

//Iterate through the names list

for(int i=0;i<names.length;i++){

//If match found, return the index else continue

if(names[i].equalsIgnoreCase(name)){

return i;

}

}

//Return -1 if match not found.

return -1;

}

//Method to load filesin array

public static String[] loadFile(String fileName){

//Create a array to hold names read from array

String[] names=new String[200];

//Variable to represent current index

int index=0;

try{

//Read file

BufferedReader br=new BufferedReader(new FileReader(fileName));

String line="";

//Read file line by line

while((line=br.readLine())!=null){

//Store line in names array

names[index]=line;

//increment index to hold next index

index++;

}

//close reader

br.close();

}catch(Exception e){

e.printStackTrace();

}

//Create a new string array to hold the number of names present in array names, eliminating null values

//, becaue the size of names array was 200, and it may be the case that file doesn't have 200 names leading to null values

//in empty indices.

//index now represents the length of new array, because it is equal to total names present in file

String[] finalNames=new String[index];

//Store values in finalNames from names

for(int i=0;i<index;i++){

finalNames[i]=names[i];

}

//Return finalNames

return finalNames;

}

}

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