I’m new to Java and I’m trying to make a method called searchListBySurName that accepts a String argument and returns true or false depending on whether a name exists with that respective last name.
Here is what I have so far:
public boolean searchListBySurName(String SurName)
{
if (list.contains(SurName)) {
return true;
} else {
return false;
}
}
Here is the test data I’m using to test the method:
public void testSearchListBySurName() {
r.addName(new Name("Joe", "Bloggs"));
r.addName(new Name("Fred", "Jones"));
assertTrue("First search should find Jones, i.e. return true", r.searchListBySurName(new String("Jones")));
r = new Register();
r.addName(new Name("Joe", "Bloggs"));
r.addName(new Name("Fred", "Wayne"));
assertFalse("Second search should not find Jones, i.e. return false", r.searchListBySurName(new String("Jones")));
}
But, the test keeps coming back as failed. What mistake am I making and how can I fix it?