我这样做了吗? ArrayList搜索

时间:2014-04-23 01:46:21

标签: java arraylist

我有一个switch语句,允许您在数据库中搜索用户。搜索基本上只是搜索匹配的字符,无论是数字还是字母。它似乎没有工作,任何想法?

这是main.java

package hartman;

import java.util.ArrayList;
import java.util.Scanner;

public class Main {

public static void main(String[] args) {
    Printer.printWelcome();

    Scanner keyboard = new Scanner(System.in);
    ArrayList<Person> personList = new ArrayList<>();
    boolean keepRunning = true;
    while (keepRunning) {
        Printer.printMenu();
        Printer.printPrompt("Please enter your operation: ");

        String userSelection = keyboard.nextLine();

        switch (userSelection) {
        case "1":
            Database.addPerson(personList);
            break;
        case "2":
            Database.printDatabase(personList);
            break;
        case "3":
            Printer.printSearchPersonTitle();
            String searchFor = keyboard.nextLine();
            Database.findPerson(searchFor);
            break;
        case "4":
            keepRunning = false;
            break;
        default:
            break;
        }
    }

    Printer.printGoodBye();
    keyboard.close();
}

}

这是Database.java

package hartman;

import java.util.ArrayList;
import java.util.Scanner;

public class Database {
static Scanner keyboard = new Scanner(System.in);
private static ArrayList<Person> personList = new ArrayList<Person>();

public Database() {

}

public static void addPerson(ArrayList<Person> personList) {
    Printer.printAddPersonTitle();
    Printer.printPrompt("  Enter first name: ");
    String addFirstName = keyboard.nextLine();

    Printer.printPrompt("  Enter last Name: ");
    String addLastName = keyboard.nextLine();

    Printer.printPrompt("  Enter social Security Number: ");
    String addSocial = keyboard.nextLine();

    Printer.printPrompt("  Enter year of birth: ");
    int addYearBorn = Integer.parseInt(keyboard.nextLine());

    System.out.printf("\n%s, %s saved!\n", addFirstName, addLastName);
    Person person = new Person();
    person.setFirstName(addFirstName);
    person.setLastName(addLastName);
    person.setSocialSecurityNumber(addSocial);
    person.setYearBorn(addYearBorn);
    personList.add(person);
}

public static void printDatabase(ArrayList<Person> personList) {
    System.out
            .printf("\nLast Name           First Name           Social Security Number  Age\n");
    System.out
            .printf("=================== ===================  ======================  ===\n");
    for (Person p : personList) {

        System.out.printf("%-20s%-21s%-24s%s\n", p.getLastName(),
                p.getLastName(), p.getSocialSecurityNumber(), p.getAge());

    }

}

public static ArrayList<Person> findPerson(String searchFor) {
    ArrayList<Person> matches = new ArrayList<>();
    for (Person p : personList) {
        boolean isAMatch = false;

        if (p.getFirstName().equalsIgnoreCase(searchFor)) {
            isAMatch = true;

        }
        if (p.getLastName().equalsIgnoreCase(searchFor)) {
            isAMatch = true;

        }
        if (p.getSocialSecurityNumber().contains(searchFor)) {
            isAMatch = true;
            ;
        } else if (String.format("%d", p.getAge()).equals(searchFor))
            if (isAMatch) {

            }
        matches.add(p);
        Printer.printPersonList(matches);
    }
    return matches;

}

 }

这是打印机正在做的事情。

public static void printPersonList(ArrayList<Person> personListToPrint) {

    System.out
            .printf("\nLast Name           First Name           Social Security Number  Age\n");
    System.out
            .printf("=================== ===================  ======================  ===\n");
    for (Person p : personListToPrint) {

        System.out.printf("%-20s%-21s%-24s%s\n", p.getLastName(),
                p.getLastName(), p.getSocialSecurityNumber(), p.getAge());

    }

2 个答案:

答案 0 :(得分:0)

你不应该像你正在做的那样在任何地方传递personList,你没有迭代你认为你正在做的personList的副本。由于personList为空,因此永远不会调用printPersonList。这就是你没有输出的原因。

根本没有理由在Main.java中使用personList。在编写代码时,您将使用的personList副本是:

private static ArrayList<Person> personList = new ArrayList<Person>();
在Database.java中

我可能会建议进一步重构,但我认为这应该足以让你获得一些有效的代码。

答案 1 :(得分:0)

在方法 findPerson 中,变量 isAMatch 似乎没有花费精力。

无论isAMatch的值是多少,列表matches都会添加对象p
所以,它可能看起来像这样:

if (isAMatch) {
     matches.add(p);
}

此外,代码Printer.printPersonList(matches);应该不在for循环中。

顺便说一下,有什么意义?

else if (String.format("%d", p.getAge()).equals(searchFor))
        if (isAMatch) {

        }