user3562527
user3562527

Reputation: 9

Passing data to a method

I am trying to pass "personListToPrint" to main.java and have it come up when switch 3 is used, however it will not work. I am not sure how to go about fixing this. Any ideas??

here is 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();
  }

 }

and here is 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;

}

}

and last, here is 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());

    }

}
}

Upvotes: 0

Views: 157

Answers (4)

Greg
Greg

Reputation: 192

I think I have your confusion figured out, but explaining it is a tad tricky.

In your Printer class, you have the following method declaration:

public static void printPersonList(ArrayList<Person> personListToPrint)

In this case, personListToPrint is the name of the variable you'll use INSIDE this method to refer to the ACTUAL ArrayList that is passed to the method when it's called from the external source.

In your case, you are calling it using this invocation within your Case "3" block:

    Printer.printPersonList(personListToPrint);

Even though the method calls it "personListToPrint," when you make the above call, you really want to pass in personList. Within Main, personList refers to some ArrayList, let's say at some memory location defined as "0100." When Printer.printPersonList is invoked, you want to pass in the array list you want printed, in this case personList (residing at memory "0100"). Once inside of printPersonList, the ArrayList stored at "0100" will be referred to using the variable name personListToPrint. It's two different names for the same memory location.

The other person who commented about the fact that you never declared or instantiated personListToPrint is right, so when you call printPersonList(personListToPrint) the way you are doing now, you're passing in a null since that particular variable was never declared or instantiated (you should actually get a compile error).

If this is confusing, comment back and I'll try to explain in a different way.

Upvotes: 0

aleks.n.fedorov
aleks.n.fedorov

Reputation: 332

You did not define variable

personListToPrint

Replace case 3 with next

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

Now is compilable

Upvotes: 1

user3536531
user3536531

Reputation: 1

your calling printer directly without having created an object of the class.

Printer printerObj = new Printer();

Upvotes: 0

tristantech
tristantech

Reputation: 66

Printer.printPersonList(personListToPrint);

Where is it supposed to come from? personListToPrint is never defined anywhere. (I Ctrl-F searched your code samples and it's nowhere)

Upvotes: 0

Related Questions