batsta13
batsta13

Reputation: 539

Java search input

I need to create a JTextField search where users can enter input and matching entries in my arraylist of objects are returned in a JTextArea.

How can I return entries even if they have only entered part of say a movie title. For example, if the user enters only an "a" how can i return all entries within the ArrayList that begin with an a e.g abducted, aeon flux. Another example would be if the ArrayList contained a title "Harry Potter" how would i return this entry if the user only enters "harryp". It needs to work regardless of case.

Upvotes: 0

Views: 168

Answers (2)

lopisan
lopisan

Reputation: 7820

You need to iterate the ArrayList and check for a similarity of your input and iterated item. You can do it simple or more complex. The most simple would be make both strings lowercase and check for a substring.

If you want to have corrected misspelling you can use Levenstein distance which tells you, how many characters you need to change in one string to get the other (and tolerate for example two misspells) - see Apache commons function getLevenshteinDistance().

For more insight see this answer.

Upvotes: 0

Djon
Djon

Reputation: 2260

You could work with toUpperCase() and toLowerCase() if you want it to be case unsensitive, and startsWith().

If you really want "harryp" (without space) to match "Harry Potter", then you'll have to delete all the spaces in the entries before comparing the input.

If you provide some code, I can show you how this works.

Upvotes: 1

Related Questions