JAVA PROGRAM PLEASE PLEASE PLEASE MODIFY THIS PROGRAM SO WHEN I UPLOAD IT TO HYPERGRADE IT RUNS AND PASSES THE TEST BECAUSE IT DOES NOT RUN AND DOES NOT PASS THE TEST CASE IN HYPERGRADE. THEN GET RID OF THE FOLLOWING FROM THE PROGRAM print "Boynames.txt is missing."; print "Girlnames.txt is missing."; and Both data files are missing." ALSO MAKE SURE IT PRINTS OUT THE FOLLOWING TEST CASE: 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 IT HAS TO SAY Enter a name to search or type QUIT to exit:\n THEH I PUT THE NAME ANNABELLE THEN IT PRINTS OUT: The name 'Annabelle' was not found in either list.\n THEN IT REPEATS Enter a name to search or type QUIT to exit:\n THEN I PUT THE NAME xavier THEN IT PRINTS OUT: The name 'Xavier' was found in popular boy names list (line 81).\n AND SO ON. UNTIL THE LAST IT HAS TO SAY Enter a name to search or type QUIT to exit:\n THEN I PUT QUIT IT ENDS THE TEST CASE. THE INPUTS ARE ALREADY IN HYPERGRADE SO MAKE SURE THIS PROGRAM PASSES THE TEST CASE WHEN I UPLOAD IT TO HYPERGRADE and take out the following from the program please: Boynames.txt is missing.\n Girlnames.txt is missing.\n Both data files are missing.\n 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; List girlNames; 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 PROGRAM
PLEASE PLEASE PLEASE MODIFY THIS PROGRAM SO WHEN I UPLOAD IT TO HYPERGRADE IT RUNS AND PASSES THE TEST BECAUSE IT DOES NOT RUN AND DOES NOT PASS THE TEST CASE IN HYPERGRADE. THEN GET RID OF THE FOLLOWING FROM THE PROGRAM print "Boynames.txt is missing."; print "Girlnames.txt is missing."; and Both data files are missing." ALSO MAKE SURE IT PRINTS OUT THE FOLLOWING TEST CASE:
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
THE INPUTS ARE ALREADY IN HYPERGRADE SO MAKE SURE THIS PROGRAM PASSES THE TEST CASE WHEN I UPLOAD IT TO HYPERGRADE and take out the following from the program please: Boynames.txt is missing.\n Girlnames.txt is missing.\n Both data files are missing.\n 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;
List<String> girlNames;
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();
}
}
Trending now
This is a popular solution!
Step by step
Solved in 4 steps with 3 images