You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Java入门课程编程挑战:姓名查询逻辑错误求助

Hey there! Let's figure out why your "Jacob" check isn't working and fix this up step by step. 😊

Core Issue Breakdown

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 boyNames and girlNames lists before running the check.

内容的提问来源于stack exchange,提问作者rwitk

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 10:16:55