将数据传递给方法

时间:2014-04-24 03:02:26

标签: java arrays methods arraylist

我正试图传递" personListToPrint"到main.java并在使用开关3时出现,但它不起作用。我不知道如何解决这个问题。任何想法??

这里是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);
            Printer.printPersonList(personListToPrint);
            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;

}

}

最后,这是printer.java

package hartman;

import java.util.ArrayList;

public class Printer {
public static void printWelcome() {
    System.out.printf("WELCOME TO PERSON DATABASE!\n");
}

public static void printGoodBye() {
    System.out.printf("\nGOOD BYE!!\n");
}

public static void printMenu() {
    System.out.printf("\nMain Menu\n");
    System.out.printf("---------\n\n");
    System.out.printf("  1. Add a new Person to the database.\n");
    System.out.printf("  2. Print the database.\n");
    System.out.printf("  3. Search for a person in the database.\n");
    System.out.printf("  4. Exit the application.\n");
    System.out.printf("\n");
}

public static void printPrintMenu() {
    System.out.printf("Print\n\n");
}

public static void printAddPersonTitle() {
    System.out.printf("\nAdd Person to Database\n\n");

}

public static void printPrompt(String promptForWhat) {
    System.out.printf("%s", promptForWhat);
}

public static void printPersonSaved(Person personSaved) {
    System.out.printf("%s", personSaved);
}

public static void printSearchPersonTitle() {
    System.out.printf("\nSearch for Person in Database\n\n");
    System.out.printf("Enter search value: ");
}

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());

    }

}
}

4 个答案:

答案 0 :(得分:1)

您没有定义变量

  

personListToPrint

将案例3替换为下一个

case "3":
    Printer.printSearchPersonTitle();
    String searchFor = keyboard.nextLine();
    ArrayList<Person> personListToPrint = Database.findPerson(searchFor);
    Printer.printPersonList(personListToPrint);
    break;

现在是可编辑的

答案 1 :(得分:0)

Printer.printPersonList(personListToPrint);

它应该来自哪里? personListToPrint永远不会在任何地方定义。 (我按Ctrl-F搜索了您的代码示例并且它没有任何地方)

答案 2 :(得分:0)

直接调用打印机而不创建类的对象。

Printer printerObj = new Printer();

答案 3 :(得分:0)

我想我已经弄清楚了你的困惑,但解释它有点棘手。

在您的Printer类中,您有以下方法声明:

public static void printPersonList(ArrayList<Person> personListToPrint)

在这种情况下,personListToPrint是您将使用INSIDE此方法的变量的名称来引用从外部源调用时传递给该方法的ACTUAL ArrayList。

在您的情况下,您在Case&#34; 3&#34;中使用此调用来调用它。块:

    Printer.printPersonList(personListToPrint);

即使该方法将其称为&#34; personListToPrint,&#34;当你进行上述调用时,你真的想传入personList。在Main中,personList引用一些ArrayList,让我们说在某些内存位置定义为&#34; 0100。&#34;当调用Printer.printPersonList时,您希望传入要打印的数组列表,在本例中为personList(驻留在内存&#34; 0100&#34;)。一旦进入printPersonList,则存储在&#34; 0100&#34;将使用变量名称personListToPrint引用。它是同一内存位置的两个不同名称。

另一个评论过你从未声明或实例化过personListToPrint这一事实的人是正确的,所以当你以现在的方式调用printPersonList(personListToPrint)时,你传递的是null,因为那个特定的变量是从未声明或实例化(实际上你应该得到一个编译错误)。

如果这令人困惑,请回复评论,我会尝试以不同的方式解释。