0

I'm having trouble making a loop or even if statement to search through an array and then if it returns the value of it, it displays it along with the word before and after.

For example:

String Criminals[] = {
    "John Doe, Theft, 1990",
    "Marry Al, Arson, 1970",
    "Sal Don, Theft, 1961"
};

it searchs for theft and then it will get the name and date as well, and find the rest if there are any: and the output in this case would be:

Results (2):

John Doe, Theft, 1990
Sal Don, Theft, 1961
3
  • Please post the code that is not working Commented Nov 28, 2014 at 19:04
  • You can use HashMap. You can specify its value and key to find it. Commented Nov 28, 2014 at 19:10
  • 1
    Looks like a Custom class instead of a String will help you a lot Commented Nov 28, 2014 at 19:13

1 Answer 1

1

for loop to iterate the array and String.contains to see if items contain your search term. The only vaguely interesting part is converting all the strings involved to a particular letter case; e.g., lower case, in order to make the search case insensitive.

String searchTerm = ...;
searchTerm = searchTerm.toLowerCase();
for (String s : Criminals) { // loop
    if (s.toLowerCase().contains(searchTerm)) { // find
        System.out.println(s); // display
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks so much! Worked perfectly.

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.