AVA PROGRAM PLEASE MODIFY THIS PROGRAM and remove Boynames.txt is missing.\n Girlnames.txt is missing.\n Both data files are missing.\n from this program please. THE TEST CASE DOES NOT ASK FOR IT. PLEASE REMOVE IT. I HAVE PROVIDED THE TEST CASE THAT FAILED AND THE INPUTS AS A SCREENSHOT. 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 boolean boyFileExists = false; boolean girlFileExists = false; try { boyNames = loadFileToList("Boynames.txt"); boyFileExists = true; } catch (FileNotFoundException e) { System.out.println("Boynames.txt is missing."); } try { girlNames = loadFileToList("Girlnames.txt"); girlFileExists = true; } catch (FileNotFoundException e) { System.out.println("Girlnames.txt is missing."); } if (!boyFileExists && !girlFileExists) { System.out.println("Both data files are 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
PLEASE MODIFY THIS PROGRAM and remove Boynames.txt is missing.\n Girlnames.txt is missing.\n
Both data files are missing.\n from this program please. THE TEST CASE DOES NOT ASK FOR IT. PLEASE REMOVE IT. I HAVE PROVIDED THE TEST CASE THAT FAILED AND THE INPUTS AS A SCREENSHOT.
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
boolean boyFileExists = false;
boolean girlFileExists = false;
try {
boyNames = loadFileToList("Boynames.txt");
boyFileExists = true;
} catch (FileNotFoundException e) {
System.out.println("Boynames.txt is missing.");
}
try {
girlNames = loadFileToList("Girlnames.txt");
girlFileExists = true;
} catch (FileNotFoundException e) {
System.out.println("Girlnames.txt is missing.");
}
if (!boyFileExists && !girlFileExists) {
System.out.println("Both data files are 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 5 steps with 3 images