JAVA PROGRAM MODIFY AND CHANGE THIS PROGRAM FOR THE FOLLOWING: IT MUST PASS THE TEST CASE WHEN I UPLOAD IT TO HYPERGRADE. 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()); // Convert names to lowercase } } } return namesList; } private static Integer searchNameInList(String name, List namesList) { name = name.toLowerCase(); // Convert user input to lowercase int index = namesList.indexOf(name); return index == -1 ? null : index + 1; } public static void main(String[] args) { List boyNames; List girlNames; boolean boyFileExists = false; boolean girlFileExists = false; try { boyNames = loadFileToList("Boynames.txt"); boyFileExists = true; } catch (FileNotFoundException e) { boyNames = new ArrayList<>(); } try { girlNames = loadFileToList("Girlnames.txt"); girlFileExists = true; } catch (FileNotFoundException e) { girlNames = new ArrayList<>(); } Scanner scanner = new Scanner(System.in); while (true) { System.out.print("Enter a name to search or type QUIT to exit:\n"); String input = scanner.nextLine().trim(); if (input.equalsIgnoreCase("QUIT")) { break; } Integer boyIndex = searchNameInList(input, boyNames); Integer girlIndex = searchNameInList(input, girlNames); if (boyIndex == null && girlIndex == null) { System.out.println("The name '" + input + "' was not found in either list."); } else { if (boyIndex != null && girlIndex != null) { System.out.println("The name '" + input + "' was found in both lists: boy names (line " + boyIndex + ") and girl names (line " + girlIndex + ")."); } else if (boyIndex != null) { System.out.println("The name '" + input + "' was found in popular boy names list (line " + boyIndex + ")."); } else { System.out.println("The name '" + input + "' was found in popular girl names list (line " + girlIndex + ")."); } } } scanner.close();
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()); // Convert names to lowercase
}
}
}
return namesList;
}
private static Integer searchNameInList(String name, List<String> namesList) {
name = name.toLowerCase(); // Convert user input to lowercase
int index = namesList.indexOf(name);
return index == -1 ? null : index + 1;
}
public static void main(String[] args) {
List<String> boyNames;
List<String> girlNames;
boolean boyFileExists = false;
boolean girlFileExists = false;
try {
boyNames = loadFileToList("Boynames.txt");
boyFileExists = true;
} catch (FileNotFoundException e) {
boyNames = new ArrayList<>();
}
try {
girlNames = loadFileToList("Girlnames.txt");
girlFileExists = true;
} catch (FileNotFoundException e) {
girlNames = new ArrayList<>();
}
Scanner scanner = new Scanner(System.in);
while (true) {
System.out.print("Enter a name to search or type QUIT to exit:\n");
String input = scanner.nextLine().trim();
if (input.equalsIgnoreCase("QUIT")) {
break;
}
Integer boyIndex = searchNameInList(input, boyNames);
Integer girlIndex = searchNameInList(input, girlNames);
if (boyIndex == null && girlIndex == null) {
System.out.println("The name '" + input + "' was not found in either list.");
} else {
if (boyIndex != null && girlIndex != null) {
System.out.println("The name '" + input + "' was found in both lists: boy names (line " + boyIndex + ") and girl names (line " + girlIndex + ").");
} else if (boyIndex != null) {
System.out.println("The name '" + input + "' was found in popular boy names list (line " + boyIndex + ").");
} else {
System.out.println("The name '" + input + "' was found in popular girl names list (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