JAVA PROGRAM PLEASE MODIFY THIS PROGRAM SO WHEN I UPLOAD IT TO HYPERGRADE IT PASSES THE TEST CASE and take out the following from the program please: Boynames.txt is missing.\n Girlnames.txt is missing.\n Both data files are missing.\n THE TEST CASE DOES NOT ASK FOR IT. PLEASE REMOVE IT. I HAVE PROVIDED THE TEST CASE THAT FAILED AND THE INPUTS AS A SCREENSHOT. import java.io.*; import java.util.*; public class NameSearcher { private static Map loadFileToMap(String filename) throws FileNotFoundException { Map namesMap = new HashMap<>(); File file = new File(filename); if (!file.exists()) { throw new FileNotFoundException(filename); } try (Scanner scanner = new Scanner(file)) { int lineNumber = 1; while (scanner.hasNext()) { String line = scanner.next().trim().toLowerCase(); namesMap.put(line, lineNumber); lineNumber++; } } return namesMap; } private static Integer searchNameInMap(String name, Map namesMap) { return namesMap.get(name.toLowerCase()); } public static void main(String[] args) { Map boyNames = new HashMap<>(); Map girlNames = new HashMap<>(); try { boyNames = loadFileToMap("Boynames.txt"); } catch (FileNotFoundException e) { // Handle exception as needed } try { girlNames = loadFileToMap("Girlnames.txt"); } catch (FileNotFoundException e) { // Handle exception as needed } Scanner scanner = new Scanner(System.in); while (true) { System.out.println("Enter a name to search or type QUIT to exit: "); String input = scanner.nextLine().trim(); if (input.equalsIgnoreCase("QUIT")) { break; } Integer boyIndex = searchNameInMap(input, boyNames); Integer girlIndex = searchNameInMap(input, girlNames); String capitalizedInput = Character.toUpperCase(input.charAt(0)) + input.substring(1).toLowerCase(); if (boyIndex == null && girlIndex == null) { System.out.println("The name '" + capitalizedInput + "' was not found in either list."); } else if (boyIndex != null && girlIndex == null) { System.out.println("The name '" + capitalizedInput + "' was found in popular boy names list (line " + boyIndex + ")."); } else if (boyIndex == null && girlIndex != null) { System.out.println("The name '" + capitalizedInput + "' was found in popular girl names list (line " + girlIndex + ")."); } else { System.out.println("The name '" + capitalizedInput + "' was found in both lists: boy names (line " + boyIndex + ") and girl names (line " + girlIndex + ")."); } } scanner.close(); } } Test Case 1 Enter a name to search or type QUIT to exit:\n AnnabelleENTER The name 'Annabelle' was not found in either list.\n Enter a name to search or type QUIT to exit:\n xavierENTER The name 'Xavier' was found in popular boy names list (line 81).\n Enter a name to search or type QUIT to exit:\n AMANDAENTER The name 'Amanda' was found in popular girl names list (line 63).\n Enter a name to search or type QUIT to exit:\n jOrdAnENTER The name 'Jordan' was found in both lists: boy names (line 38) and girl names (line 75).\n Enter a name to search or type QUIT to exit:\n quitENTER
JAVA PROGRAM
PLEASE MODIFY THIS PROGRAM SO WHEN I UPLOAD IT TO HYPERGRADE IT PASSES THE TEST CASE and take out the following from the program please: Boynames.txt is missing.\n Girlnames.txt is missing.\n Both data files are missing.\n THE TEST CASE DOES NOT ASK FOR IT. PLEASE REMOVE IT. I HAVE PROVIDED THE TEST CASE THAT FAILED AND THE INPUTS AS A SCREENSHOT.
import java.io.*;
import java.util.*;
public class NameSearcher {
private static Map<String, Integer> loadFileToMap(String filename) throws FileNotFoundException {
Map<String, Integer> namesMap = new HashMap<>();
File file = new File(filename);
if (!file.exists()) {
throw new FileNotFoundException(filename);
}
try (Scanner scanner = new Scanner(file)) {
int lineNumber = 1;
while (scanner.hasNext()) {
String line = scanner.next().trim().toLowerCase();
namesMap.put(line, lineNumber);
lineNumber++;
}
}
return namesMap;
}
private static Integer searchNameInMap(String name, Map<String, Integer> namesMap) {
return namesMap.get(name.toLowerCase());
}
public static void main(String[] args) {
Map<String, Integer> boyNames = new HashMap<>();
Map<String, Integer> girlNames = new HashMap<>();
try {
boyNames = loadFileToMap("Boynames.txt");
} catch (FileNotFoundException e) {
// Handle exception as needed
}
try {
girlNames = loadFileToMap("Girlnames.txt");
} catch (FileNotFoundException e) {
// Handle exception as needed
}
Scanner scanner = new Scanner(System.in);
while (true) {
System.out.println("Enter a name to search or type QUIT to exit: ");
String input = scanner.nextLine().trim();
if (input.equalsIgnoreCase("QUIT")) {
break;
}
Integer boyIndex = searchNameInMap(input, boyNames);
Integer girlIndex = searchNameInMap(input, girlNames);
String capitalizedInput = Character.toUpperCase(input.charAt(0)) + input.substring(1).toLowerCase();
if (boyIndex == null && girlIndex == null) {
System.out.println("The name '" + capitalizedInput + "' was not found in either list.");
} else if (boyIndex != null && girlIndex == null) {
System.out.println("The name '" + capitalizedInput + "' was found in popular boy names list (line " + boyIndex + ").");
} else if (boyIndex == null && girlIndex != null) {
System.out.println("The name '" + capitalizedInput + "' was found in popular girl names list (line " + girlIndex + ").");
} else {
System.out.println("The name '" + capitalizedInput + "' was found in both lists: boy names (line " + boyIndex + ") and girl names (line " + girlIndex + ").");
}
}
scanner.close();
}
}
Test Case 1
AnnabelleENTER
The name 'Annabelle' was not found in either list.\n
Enter a name to search or type QUIT to exit:\n
xavierENTER
The name 'Xavier' was found in popular boy names list (line 81).\n
Enter a name to search or type QUIT to exit:\n
AMANDAENTER
The name 'Amanda' was found in popular girl names list (line 63).\n
Enter a name to search or type QUIT to exit:\n
jOrdAnENTER
The name 'Jordan' was found in both lists: boy names (line 38) and girl names (line 75).\n
Enter a name to search or type QUIT to exit:\n
quitENTER



Step by step
Solved in 4 steps with 1 images









