JAVA PROGRAM BELOW IS A WORKING PROGRAM. PLEASE MODIFY THIS PROGRAM SO IT PASSES THE TEST CASE. 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 { // Load a text file into a list of names (ignores case) private static List loadFileToList(String filename) throws FileNotFoundException { List namesList = new ArrayList<>(); File file = new File(filename); // Check if the file exists, if not, throw an exception if (!file.exists()) { throw new FileNotFoundException(filename); } try (Scanner scanner = new Scanner(file)) { while (scanner.hasNextLine()) { // Read each line, split by whitespace, and add names to the list in lowercase String line = scanner.nextLine().trim(); String[] names = line.split("\\s+"); for (String name : names) { namesList.add(name.toLowerCase()); } } } return namesList; } // Search for a name in a list and return its position (ignores case) 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; boolean boyFileExists = false; boolean girlFileExists = false; try { // Load boy names from a file, set boyFileExists to true if successful boyNames = loadFileToList("Boynames.txt"); boyFileExists = true; } catch (FileNotFoundException e) { // If the file is not found, initialize an empty list boyNames = new ArrayList<>(); } try { // Load girl names from a file, set girlFileExists to true if successful girlNames = loadFileToList("Girlnames.txt"); girlFileExists = true; } catch (FileNotFoundException e) { // If the file is not found, initialize an empty list 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; } // Capitalize the first letter and lowercase the rest of the input 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) { // If the name is not found in either list System.out.println("The name '" + capitalizedInput + "' was not found in either list."); } else { if (boyIndex != null && girlIndex != null) { // If the name is found in both lists System.out.println("The name '" + capitalizedInput + "' was found in both lists: boy names (line " + boyIndex + ") and girl names (line " + girlIndex + ")."); } else if (boyIndex != null) { // If the name is found in the boy names list System.out.println("The name '" + capitalizedInput + "' was found in popular boy names list (line " + boyIndex + ")."); } else { // If the name is found in the girl names list System.out.println("The name '" + capitalizedInput + "' 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. 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 {
// Load a text file into a list of names (ignores case)
private static List<String> loadFileToList(String filename) throws FileNotFoundException {
List<String> namesList = new ArrayList<>();
File file = new File(filename);
// Check if the file exists, if not, throw an exception
if (!file.exists()) {
throw new FileNotFoundException(filename);
}
try (Scanner scanner = new Scanner(file)) {
while (scanner.hasNextLine()) {
// Read each line, split by whitespace, and add names to the list in lowercase
String line = scanner.nextLine().trim();
String[] names = line.split("\\s+");
for (String name : names) {
namesList.add(name.toLowerCase());
}
}
}
return namesList;
}
// Search for a name in a list and return its position (ignores case)
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;
boolean boyFileExists = false;
boolean girlFileExists = false;
try {
// Load boy names from a file, set boyFileExists to true if successful
boyNames = loadFileToList("Boynames.txt");
boyFileExists = true;
} catch (FileNotFoundException e) {
// If the file is not found, initialize an empty list
boyNames = new ArrayList<>();
}
try {
// Load girl names from a file, set girlFileExists to true if successful
girlNames = loadFileToList("Girlnames.txt");
girlFileExists = true;
} catch (FileNotFoundException e) {
// If the file is not found, initialize an empty list
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;
}
// Capitalize the first letter and lowercase the rest of the input
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) {
// If the name is not found in either list
System.out.println("The name '" + capitalizedInput + "' was not found in either list.");
} else {
if (boyIndex != null && girlIndex != null) {
// If the name is found in both lists
System.out.println("The name '" + capitalizedInput + "' was found in both lists: boy names (line " + boyIndex + ") and girl names (line " + girlIndex + ").");
} else if (boyIndex != null) {
// If the name is found in the boy names list
System.out.println("The name '" + capitalizedInput + "' was found in popular boy names list (line " + boyIndex + ").");
} else {
// If the name is found in the girl names list
System.out.println("The name '" + capitalizedInput + "' was found in popular girl names list (line " + girlIndex + ").");
}
}
}
scanner.close();
}
}
Step by step
Solved in 4 steps with 3 images