JAVA PROGRAM BELOW IS A WORKING PROGRAM. PLEASE MODIFY THIS PROGRAM SO IT PASSES THE TEST CASE WHEN I UPLOAD IT TO HYPERGRADE BECAUSE IT KEEPS FAILING THE TEST CASE. PLEASE FIX IT NOW!!! I HAVE PROVIDED THE failed part of the test case AND THE INPUTS AS A SCREENSHOT. 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 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); } } } return namesList; } private static Integer searchNameInList(String name, List namesList) { 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(); } }
JAVA PROGRAM
BELOW IS A WORKING PROGRAM. PLEASE MODIFY THIS PROGRAM SO IT PASSES THE TEST CASE WHEN I UPLOAD IT TO HYPERGRADE BECAUSE IT KEEPS FAILING THE TEST CASE. PLEASE FIX IT NOW!!! I HAVE PROVIDED THE failed part of the test case AND THE INPUTS AS A SCREENSHOT.
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
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);
}
}
}
return namesList;
}
private static Integer searchNameInList(String name, List<String> namesList) {
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();
}
}
Step by step
Solved in 4 steps with 3 images