Java入门课程编程挑战:姓名查询逻辑错误求助
Hey there! Let's figure out why your "Jacob" check isn't working and fix this up step by step. 😊
The most likely problem here is that you're storing the entire line from the .txt file (like "1 Jacob") into your ArrayList instead of just the name itself. When you input "Jacob" to check, it can't match the full line entry, hence the false negative.
Fix 1: Correctly Extract Names from the File
Since you mentioned not using FileReader or BufferedReader, I'll assume you're using Scanner to read the files. Here's how to adjust your reading logic to grab only the name:
Bad (Original) Reading Logic
ArrayList<String> boyNames = new ArrayList<>(); Scanner fileScanner = new Scanner(new File("boynames.txt")); while (fileScanner.hasNextLine()) { // This adds the whole line ("1 Jacob") instead of just "Jacob" boyNames.add(fileScanner.nextLine()); } fileScanner.close();
Fixed Reading Logic (Two Options)
Option 1: Use nextInt() to skip the ranking, then grab the name:
ArrayList<String> boyNames = new ArrayList<>(); try { Scanner fileScanner = new Scanner(new File("boynames.txt")); while (fileScanner.hasNext()) { fileScanner.nextInt(); // Skip the numeric ranking boyNames.add(fileScanner.next()); // Grab the name (automatically skips spaces) } fileScanner.close(); } catch (FileNotFoundException e) { System.out.println("Oops! Couldn't find the boy names file."); e.printStackTrace(); }
Option 2: Split the line by spaces and take the name (great if names have no spaces):
ArrayList<String> boyNames = new ArrayList<>(); try { Scanner fileScanner = new Scanner(new File("boynames.txt")); while (fileScanner.hasNextLine()) { String line = fileScanner.nextLine(); // Split on one or more spaces, take the second element as the name String name = line.split("\\s+")[1]; boyNames.add(name); } fileScanner.close(); } catch (FileNotFoundException e) { System.out.println("Oops! Couldn't find the boy names file."); e.printStackTrace(); }
Repeat this logic for your girl names file too!
Fix 2: Improve the Name Check (Optional but User-Friendly)
Add case-insensitive checking so users can input "jacob", "JACOB", or "Jacob" and still get a correct result:
Scanner userScanner = new Scanner(System.in); System.out.print("Enter a name to check: "); String inputName = userScanner.nextLine().trim(); boolean isPopular = false; // Check boy names for (String name : boyNames) { if (name.equalsIgnoreCase(inputName)) { isPopular = true; break; } } // If not found in boys, check girls if (!isPopular) { for (String name : girlNames) { if (name.equalsIgnoreCase(inputName)) { isPopular = true; break; } } } // Output result if (isPopular) { System.out.println(inputName + " is a popular name!"); } else { System.out.println(inputName + " isn't a popular name."); } userScanner.close();
Quick Checks to Avoid Other Issues
- Make sure your .txt files are in the correct location (same folder as your Java class, or use the full file path like
"src/boynames.txt"). - Double-check that you're adding names to both
boyNamesandgirlNameslists before running the check.
内容的提问来源于stack exchange,提问作者rwitk

