使用关键字进行连续搜索

时间:2013-09-15 15:35:17

标签: java arrays search sequential

我的任务要求我制作一个犯罪记录程序,提示用户输入犯罪名称,犯罪和定罪年份,并使用其他类(数据库)将其存储在数组中。该课程如下:

class Database { 
   String name;
   String crime;
   int year;
}

我做了一个方法来输入我的信息,但当我测试它以显示列表时,它只显示我输入的最后一个罪犯。为什么要这样做?

//method to input criminal records
public static void inputData() throws IOException {
    arr = new Database[3];        
    Database temp = new Database();        

    for(int i = 0; i < arr.length; i++){
        System.out.print("Enter criminal first and last name: ");
        String name = br.readLine();
        temp.name = name;        

        System.out.print("Enter crime: ");
        String crime = br.readLine();
        temp.crime = crime;

        System.out.print("Enter the year of conviction: ");
        int year = Integer.valueOf(br.readLine()).intValue();
        temp.year = year;

        // Copy the name, crime, year to the global variables
        arr[i] = temp;
        System.out.println(arr[i].name + " was convicted in the year " + arr[i].year + " for " + arr[i].crime + ".");
    }
}

我还需要根据用户输入的关键字(犯罪类型)进行顺序搜索,然后显示犯下此类犯罪的罪犯。以下是搜索方法:

//method to search data
public static int searchData() throws IOException{
    System.out.print("Which crime would you like to select? (arson, theft, assault) ");
    String searchKey = br.readLine();

    for (int i = 0; i < arr.length; i++){
        if (arr[i].crime.equals(searchKey)){ 
            return i;  //found it!
        }
    }
    //key was not found
    return -1;
}

我的老师很难解释,所以我研究了顺序搜索,但我仍然无法弄清楚如何才能显示犯下该罪行的罪犯名单。

2 个答案:

答案 0 :(得分:4)

您要在数组中添加对同一实例的引用。您应该每次在循环中创建新实例:

// Database temp = new Database();   // Remove this

for(int i = 0; i < arr.length; i++) {
    Database temp = new Database();        

也可以,而不是直接访问Database中的字段:

temp.name = name;   

您应该使用 setters ,并将您的字段设为私有字段:

temp.setName(name);

答案 1 :(得分:0)

在本练习中,我建议您对犯罪使用枚举,以便您可以根据代码的说明添加应用程序将从用户输入中识别的其他犯罪。

public enum Crime {
    ARSON{
        public String toString(){
            return "arson";
        }
    },
    ASSAULT{
        public String toString(){
            return "assault";
        }
    },
    THEFT{
        public String toString(){
            return "theft";
        }
    };

    public static String types(){
        return ARSON + ", " + ASSAULT + ", " + THEFT;
    }
}

我不会将名称数据库用于犯罪分子,因为它具有误导性。刑事或歹徒更合适。

public class Outlaw {
    private String name;
    private Crime crime;
    private int year;

    public Outlaw(String name, Crime crime, int year){
        this.name=name;
        this.crime=crime;
        this.year=year;
    }
    public String getName() {
        return name;
    }
    public Crime getCrime() {
        return crime;
    }
    public int getYear() {
        return year;
    }
    @Override
    public String toString(){
        return new String(name + " was convicted in the year " + year + " for " + crime + ".");
    }
}

public class InputData {
    private static Scanner scanner;
    public static void main(String[] args) {
        scanner = new Scanner(System.in);
        List<Outlaw> criminals = inputData();
        showCriminals(criminals);
        showCriminalsForCrime(criminals);
        scanner.close();
    }
    public static List<Outlaw> inputData(){
        List<Outlaw> criminals = new ArrayList<Outlaw>();
        int numCriminals;

        System.out.print("How many criminals are you going to enter? ");
        numCriminals = scanner.nextInt(); scanner.nextLine();
        for(int i = 0; i < numCriminals; i++){
            criminals.add(inputOutlaw());
        }

        return criminals;
    }
    public static Outlaw inputOutlaw() {    
        System.out.print("Enter criminal first and last name: ");
        String name = scanner.nextLine();
        Crime crime = inputCrime();
        System.out.print("Enter the year of conviction: ");
        int year = scanner.nextInt(); scanner.nextLine();

        return new Outlaw(name, crime, year);
    }
    public static void showCriminalsForCrime(List<Outlaw> criminals){
        System.out.println("Search the criminals for their crime");
        Crime crime = inputCrime();
        List<Outlaw> criminalsForCrime = getCriminalsFor(crime,criminals);
        if(!criminals.isEmpty()){
            for(Outlaw criminal : criminalsForCrime){
                System.out.println(criminal.toString());
            }
        } else {
            System.out.println("There are not criminals for" + crime);
        }
    }
    public static Crime inputCrime(){
        Crime crime = null;

        while(crime == null){
            System.out.print("Which crime would you like to select? " + Crime.types() + ": ");
            try{
                crime = Crime.valueOf(scanner.nextLine().toUpperCase().trim());
            } catch(IllegalArgumentException e){
                System.out.println("The crime is not in the application!");
            }
        }

        return crime;
    }
    public static List<Outlaw> getCriminalsFor(Crime crime, List<Outlaw> criminals){
        List<Outlaw> criminalsForCrime = new ArrayList<Outlaw>();

        for(Outlaw criminal : criminals){
            if(criminal.getCrime().equals(crime)){
                criminalsForCrime.add(criminal);
            }
        }

        return criminalsForCrime;
    }
    public static void showCriminals(List<Outlaw> criminals){
        System.out.println("CRIMINALS: ");
        for(Outlaw criminal : criminals){
            System.out.println(criminal.toString());
        }
    }   
}

告诉老师更好地解释:D