0

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.

4

2 回答 2

1

如果您希望它不区分大小写,您可以使用toUpperCase()and和.toLowerCase()startsWith()

如果您真的希望“harryp”(没有空格)匹配“Harry Potter”,那么您必须在比较输入之前删除条目中的所有空格。

如果您提供一些代码,我可以向您展示这是如何工作的。

于 2013-05-29T08:53:02.380 回答
0

您需要迭代 ArrayList 并检查输入和迭代项的相似性。你可以做简单或更复杂的事情。最简单的方法是将两个字符串都设为小写并检查子字符串。

如果您想纠正拼写错误,您可以使用Levenstein 距离,它告诉您需要在一个字符串中更改多少字符才能获得另一个字符串(并容忍例如两个拼写错误) - 请参阅 Apache commons 函数getLevenshteinDistance()

有关更多信息,请参阅此答案

于 2013-05-29T08:59:25.963 回答