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

Java数组搜索异常:多匹配犯罪记录仅显示单条结果

警察数据库Java程序搜索功能修复

嘿,我瞅你这个警察数据库Java程序遇到了个头疼的问题——明明数据库里有多条同罪名的罪犯记录,搜索的时候却只显示最后一条,对吧?咱们来一步步搞定它!

程序需求回顾

  • 支持录入8名罪犯的姓名、罪名、定罪年份
  • 仅允许搜索三种罪名:Theft、Arson、Assault
  • 搜索时需要输出所有匹配该罪名的罪犯完整信息

当前问题

当搜索某一罪名时,即便存在多条匹配记录,程序仅会显示最后一条匹配的信息。

你的原始代码

//Include Libraries 
import java.io.*; 
import java.util.Scanner; 

//Class 
public class Police { 
    //Declare variables, Arrays, and Strings for future use in the program 
    static String name[] = new String[8]; //Name Array that can hold 8 names 
    static String crime[] = new String[8]; //Position Array that can hold 8 crimes 
    static int year[] = new int[8]; //Age Array that can hold 8 years 
    static int x, i; //x and i are counters 

    //Main Method 
    public static void main(String[] args) throws IOException { 
        //Open BufferedReader 
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 

        /* 
         * The program will now loop the menu 
         * options shown below. 
         * It will loop until the user exits 
         * the program. 
         */ 
        //variable for menu 
        int menuChoice = 1; 

        //Display introduction 
        System.out.print("Hello and Welcome to Police Java Database\r"); 

        while (menuChoice != 3) { 
            //Display menu choices 
            System.out.print("Enter Menu Choice\n"); 
            System.out.print("**********************"); 
            System.out.print("\r(1) => Enter Criminal Data \n"); 
            System.out.print("(2) => Display Matching Crime \n"); 
            System.out.print("(3) => Exit Program \r"); 

            //try-catch statement to read input 
            try { 
                menuChoice = Integer.parseInt(br.readLine()); 
            } catch (IOException ie) { 
                ie.printStackTrace(); 
            } 

            //switch statement to loop the menu choices 
            switch(menuChoice) { 
                case 1: 
                    //Case 1 is the store input method 
                    inputInfo(); 
                    break; 
                case 2: 
                    //Case 2 is the sort input method 
                    inputSearch(); 
                    break; 
                case 3: 
                    //exit program for case 3 
                    return; 
            } 
        } 
    } 

    //Method to store input info 
    public static void inputInfo() throws IOException { 
        //Open BufferedReader 
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); 
        //Open Scanner 
        Scanner p = new Scanner(System.in); 

        //loop to request to fill Arrays 
        for (x = 0; x < 8; x++) { 
            //Ask for criminal name input 
            System.out.print("Enter criminal name: "); 
            //Read input and store name in an Array 
            name[x] = in.readLine(); 

            //Ask for crime input 
            System.out.print("Enter crime offence: "); 
            //Read input and store crime in an Array 
            crime[x] = in.readLine(); 

            //Ask for year the crime was committed 
            System.out.print("Enter year of conviction: \r"); 
            //Read input and store year in an Array 
            year[x] = p.nextInt(); 
        } 
    } 

    //Method to select and find info 
    public static void inputSearch() throws IOException { 
        //Open BufferedReader 
        BufferedReader sc = new BufferedReader(new InputStreamReader(System.in)); 

        //declare variables that'll be used to search crime input 
        int flag; 
        boolean found; 
        String searchcrime; 

        flag = 0; 
        found = false; 

        //Ask what crime the user is searching for 
        System.out.print("What is the crime you're searching for: "); 
        //Read the input 
        searchcrime =sc.readLine(); 

        //Display which crime they selected 
        System.out.print("You searched for criminals with the offence of: " + searchcrime + "\r"); 

        //Compare all input for asked crime 
        for (x = 0; x < 8; x++) { 
            if (searchcrime.compareTo(crime[x])==0) { 
                flag = x; 
                found = true; 
            } 
        } 

        //If the input is not found, show error 
        if (found == false) { 
            System.out.print("Error! Crime not found"); 
        } else { 
            //Display each category of records; names, crime, year 
            System.out.println(" Name " + " -----" + " Crime " + "-----" + " Year "); 
            //Display matching crime with criminal name and year of conviction. 
            System.out.println( name[flag] + " --- " + crime[flag] + " --- " + year[flag]); 
        } 
    } 
}

问题根源分析

咱们看inputSearch方法里的循环:

for (x = 0; x < 8; x++) {
    if (searchcrime.compareTo(crime[x])==0) {
        flag = x;
        found = true;
    }
}

每次找到匹配的记录时,你只是把flag更新为当前的索引x,循环结束后flag只会保存最后一个匹配项的索引,所以最后只输出了最后一条匹配的记录。

另外还有个小隐患:compareTo是大小写敏感的,如果用户输入小写的arson,就匹配不到数据库里的Arson,咱们也可以顺便修复这个问题。

修复方案

修改inputSearch方法,直接在循环里输出每一条匹配的记录,而不是只记录最后一个索引。同时统一字符串的大小写,避免大小写不匹配的问题:

//Method to select and find info 
public static void inputSearch() throws IOException { 
    //Open BufferedReader 
    BufferedReader sc = new BufferedReader(new InputStreamReader(System.in)); 

    boolean found = false; 
    String searchcrime; 

    //Ask what crime the user is searching for 
    System.out.print("What is the crime you're searching for: "); 
    //Read the input,转成大写统一比较,同时去除前后空格
    searchcrime = sc.readLine().trim().toUpperCase(); 

    //Display which crime they selected 
    System.out.println("You searched for criminals with the offence of: " + searchcrime); 

    //先打印表头
    System.out.println(" Name " + " -----" + " Crime " + "-----" + " Year ");

    //Compare all input for asked crime 
    for (x = 0; x < 8; x++) { 
        //这里也转成大写+去空格,避免输入时的格式问题
        if (searchcrime.equals(crime[x].trim().toUpperCase())) { 
            //找到匹配项直接输出
            System.out.println(name[x] + " --- " + crime[x] + " --- " + year[x]); 
            found = true; 
        } 
    } 

    //If the input is not found, show error 
    if (!found) { 
        System.out.println("Error! Crime not found"); 
    } 
}

额外优化建议

  1. 在录入罪名的时候,可以增加校验逻辑,确保用户只能输入Theft、Arson、Assault这三种,避免无效数据混入
  2. 可以统一用Scanner处理所有输入,避免同时使用BufferedReader和Scanner导致的输入冲突问题

修复后的示例效果

示例输入:

Bugs Malone, Theft, 1981
Slippery Sal, Theft, 1991
Al Copone, Assault, 1947
Eddie Striker, Arson, 1969
Tom Tuner, Arson, 2005
Gared Gipson,Assault, 1998
Jary Lucky, Arson, 2013
Lizzy Patty, Theft, 2009

搜索Arson的输出:

What is the crime you're searching for: Arson
You searched for criminals with the offence of: ARSON
 Name  ----- Crime ----- Year 
Eddie Striker --- Arson --- 1969
Tom Tuner --- Arson --- 2005
Jary Lucky --- Arson --- 2013

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.29 07:08:33