0

I am trying to search for the value in an array. The results keep coming back as nothing though. I am assuming my loop isnt proper, could you assist me in what i am doing wrong?? It is suppose to use the value held in "searchFor" and match up. Then display the value in Printer.printPersonList.

    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;

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

        } else 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;

}

This is from printer.java

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

    }
1
  • 1
    else if (String.format("%d", p.getAge()).equals(searchFor)) has no meaningful body. Commented Apr 24, 2014 at 22:17

3 Answers 3

2

The problem with your algorithm is that you are setting isAMatch as true if the Person's first name is equal to the key that you are searching for, but not adding the person to the list. Here's the segment that's flawed:

...code omitted...

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

                matches.add(p);
            }

        Printer.printPersonList(matches);

    }

...code omitted...

The if(isAMatch) code is only executed if String.format("%d", p.getAge()).equals(searchFor) - there are no open/close parentheses afterwards to indicate a code block.

Sign up to request clarification or add additional context in comments.

Comments

1

It looks like you're missing some code. Right now, a match will only be added if the age matches and isAMatch is already true. You'll probably want to change

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

to

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

3 Comments

still displays nothing :(
At this point you'll need to determine whether personList contains any items, and ensure that your search should find something by checking the data in each Person.
0

Your code should be like that :

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;

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

        } else if (p.getSocialSecurityNumber().contains(searchFor)) {
            isAMatch = true;

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

        if (isAMatch) {

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

}

I modified the last else if statement, and displaced the Printer method call out of the for loop.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.