JAVA PROGRAM PLEASE MODIFY THE PROGRAM BECAUSE WHEN I UPLOAD IT TO HYPEREGRADE IT DOES NOT PASS THE TEST CASE. IT SAYS 0 OUT OF 1 PASSED. I DO NOT NEED BOYNAMES.TXT IS MISSING AND Exiting program. Goodbye IN THE PROGRAM. I ONLY NEED IT TO READ BOYNAMES.TXT AND GIRLNAMES.TXT AND DISPLAY THE TEST CASE. ALSO, THE PROGRAM MUST DISPLAY THE FOLLOWING OUTPUT FOR TEST CASE WHICH ARE LISTED BELOW: 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 I HAVE PROVIDED THE FAILED TEST CASES AS A SCREENSHOT AND THE INPUTS FOR THE PROGRAM. 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()) { namesList.add(scanner.nextLine().trim().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; List girlNames; try { boyNames = loadFileToList("Boynames.txt"); girlNames = loadFileToList("Girlnames.txt"); } catch (FileNotFoundException e) { System.out.println(e.getMessage() + " is missing."); return; } 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(); } }
JAVA PROGRAM
PLEASE MODIFY THE PROGRAM BECAUSE WHEN I UPLOAD IT TO HYPEREGRADE IT DOES NOT PASS THE TEST CASE. IT SAYS 0 OUT OF 1 PASSED. I DO NOT NEED BOYNAMES.TXT IS MISSING AND Exiting program. Goodbye IN THE PROGRAM. I ONLY NEED IT TO READ BOYNAMES.TXT AND GIRLNAMES.TXT AND DISPLAY THE TEST CASE. ALSO, THE PROGRAM MUST DISPLAY THE FOLLOWING OUTPUT FOR TEST CASE WHICH ARE LISTED BELOW:
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
I HAVE PROVIDED THE FAILED TEST CASES AS A SCREENSHOT AND THE INPUTS FOR THE PROGRAM. THANK YOU.
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()) {
namesList.add(scanner.nextLine().trim().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;
List<String> girlNames;
try {
boyNames = loadFileToList("Boynames.txt");
girlNames = loadFileToList("Girlnames.txt");
} catch (FileNotFoundException e) {
System.out.println(e.getMessage() + " is missing.");
return;
}
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();
}
}
Step by step
Solved in 4 steps with 4 images