JAVA PROGRAM MODIFY AND CHANGE THIS PROGRAM EVEN MORE FOR THE FOLLOWING: IT MUST PASS THE TEST CASE WHEN I UPLOAD IT TO HYPERGRADE. I EVEN CHANGED THE TEXT FILES NAMES. I HAVE ALSO PROVIDED THE BOYMNAMES.TXT AND GIRLNAMES.TXT WHICH ARE INPUTS AND THE FAILED TEST CASES AS A SCREENSHOT. THANK YOU import java.io.*; import java.util.*; public class NameSearcher { private static List loadFileToList(String filename) throws FileNotFoundException { List namesList = new ArrayList<>(); File file = new File(filename); if (!file.exists()) { throw new FileNotFoundException(filename); } try (Scanner scanner = new Scanner(file)) { while (scanner.hasNextLine()) { String line = scanner.nextLine().trim(); String[] names = line.split("\\s+"); for (String name : names) { namesList.add(name.toLowerCase()); } } } return namesList; } private static Integer searchNameInList(String name, List namesList) { int index = namesList.indexOf(name.toLowerCase()); return index == -1 ? null : index + 1; } public static void main(String[] args) { List boyNames = new ArrayList<>(); // Initialize with an empty list List girlNames = new ArrayList<>(); // Initialize with an empty list try { boyNames = loadFileToList("BoyNames.txt"); } catch (FileNotFoundException e) {} try { girlNames = loadFileToList("GirlNames.txt"); } catch (FileNotFoundException e) {} 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; } String capitalizedInput = input.substring(0, 1).toUpperCase() + input.substring(1).toLowerCase(); Integer boyIndex = searchNameInList(input, boyNames); Integer girlIndex = searchNameInList(input, girlNames); 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
import java.io.*;
import java.util.*;
public class NameSearcher {
private static List<String> loadFileToList(String filename) throws FileNotFoundException {
List<String> namesList = new ArrayList<>();
File file = new File(filename);
if (!file.exists()) {
throw new FileNotFoundException(filename);
}
try (Scanner scanner = new Scanner(file)) {
while (scanner.hasNextLine()) {
String line = scanner.nextLine().trim();
String[] names = line.split("\\s+");
for (String name : names) {
namesList.add(name.toLowerCase());
}
}
}
return namesList;
}
private static Integer searchNameInList(String name, List<String> namesList) {
int index = namesList.indexOf(name.toLowerCase());
return index == -1 ? null : index + 1;
}
public static void main(String[] args) {
List<String> boyNames = new ArrayList<>(); // Initialize with an empty list
List<String> girlNames = new ArrayList<>(); // Initialize with an empty list
try {
boyNames = loadFileToList("BoyNames.txt");
} catch (FileNotFoundException e) {}
try {
girlNames = loadFileToList("GirlNames.txt");
} catch (FileNotFoundException e) {}
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;
}
String capitalizedInput = input.substring(0, 1).toUpperCase() + input.substring(1).toLowerCase();
Integer boyIndex = searchNameInList(input, boyNames);
Integer girlIndex = searchNameInList(input, girlNames);
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 3 steps with 1 images