Instead of returning 0..1 object of class PhoneEntry you want to return 0..* entries (where * stands for any number > 0). This could be done as an array but would be elegant since the number of matching results isn't a-priori known. For cases like that Java provides collections. E.g. consider to use java.util.List for this purpose, like so
List<PhoneEntry> search( String targetName1, String targetName2 )
if you use generics or else
List search( String targetName1, String targetName2 )
if not (but I recommend generics). Now, in cases where you have found a match, you have not to return immediately but to append it to the list (which is initially empty after creation) and return the list first after all matches are found. Printing the result then obviously needs an iteration over the list (see java.util.List.iterator() and java.util.Iterator).
Your code as is only works if it is guaranteed that unspecified names are the empty string (and not null or otherwise NullPoinerException will occur). Then you have 3 cases: (1) Both the firstname and the lastname are not the empty string and you test for both. (2) Firstname is the empty string but lastname is not, then you test for the latter one only. (3) Firstname is not empty but lastname is, then you test for the former one only.
So each of the 3 case distinctions is complete and hence the "else" clauses are superfluous. They are not superfluous if you use reduced conditions, e.g. this way:
List<PhoneEntry> result = new List<PhoneEntry>();targetName1 = targetName1.toUpperCase();targetName2 = targetName2.toUpperCase();if(targetName1.length()>0 && targetName2.length()>0) { // iterate phonebook and test for both firstname and lastname, append on match} else if(targetName1.length()>0) { // iterate phonebook and test for firstname only, append on match} else if(targetName2.length()>0) { // iterate phonebook and test for lastname only, append on match}return result;
[Edited by - haegarr on April 10, 2007 1:24:18 PM]