Welcome to the CSC Q&A, on our server named in honor of Ada Lovelace. Write great code! Get help and give help!
It is our choices... that show what we truly are, far more than our abilities.


+7 votes
asked in CSC212_Spring2019 by (1 point)

1 Answer

+4 votes

The search method needs to ignore case, but it doesn't use equalIgnoreCase since we are not checking if two strings are equal.

You want to check if one string (the name in the nameRecord) contains the string which is the partial name entered by the user. The issue is that the contains method checks only for the exact string including the case of the characters. To get the contains method to work for us, we need to use it on strings that are either both uppercase or both lowercase to "force" the case to be of no consequence.

Let's say the partial name entered by the user was stored in the String partial. Then the statement

partial = partial.toLowerCase();

will store the lowercase version into the same variable overwriting whatever version the user entered.

Then as you loop through the nameList array, for each name case be stored into a temporary String variable and that changed to lower case. Use a temporary string because we don't want to actually change the name stored in nameList.

String currentName = nameList[i].getName();
currentName = currentName.toLowerCase();

Finally, use the contains method on the lowercase versions.

if (currentName.contains(partial)){......
answered by (1 point)