4

I want to match an exact string in an ArrayList<String>.

Currently this code will execute if myArrayList.contains(wordImLookingFor).

if (myArrayList.contains(exactWordImLookingFor)) {
    Toast.makeText(getApplicationContext(), "Match", Toast.LENGTH_SHORT).show();
}

In summary, I'm looking for the code to execute when the entire String "dude" is entered, not just "d" or "du" or "dud".

  • 2
    contains must do exact match, explain its behavior more – jmj Jul 15 '14 at 17:20
  • I take the input from the user, run a onTextChangedListener to see if it matches, then run a method. What always happens is it is executed right away. Say "dude" is in my `arraylist`, when the user enters "d" it runs the code.. –  Jul 15 '14 at 17:29
  • inspect your input `exactWordImLookingFor` before contains method – jmj Jul 15 '14 at 17:30
  • String wordImLookingFor = EditText.getText().toString().toUpperCase(); –  Jul 15 '14 at 17:31
  • what are you trying to say with this statement ? – jmj Jul 15 '14 at 17:33
  • i do not agree with "Currently this code will execute if myArrayList.contains(wordImLookingFor)." – skoll Jul 15 '14 at 17:53
  • @skoll Why wouldn't it? That's literally the exact condition of the if statement. – Akshay Jul 15 '14 at 17:57
  • no, there is not "exact" in it – skoll Jul 16 '14 at 15:26

5 Answers5

3

If you want to see if a string is in the arraylist try this:

for (String s : myArrayList)
{
    if (s.equals(wordImLookingFor))
    {
        // Run your code here
    }
}

or

if (myArrayList.contains(wordImLookingFor))
{
    // Run your code here
}

If you want to see if a string entered is a substring of anything in the arraylist, try this:

for (String s : myArrayList)
{
    if (s.contains(wordImLookingFor))
    {
        // Run your code here
    }
}

This should work for your example of myArrayList containing "dude" and the user inputting "d".

Akshay
  • 814
  • 6
  • 19
  • 1
    Thanks, but I'm looking for the code to execute when the entire `String` "dude" is entered, not just "d" or "du" or "dud" –  Jul 15 '14 at 17:46
  • You said that backwards in your comment. You should edit it and the original question. – Akshay Jul 15 '14 at 17:49
  • Man, I've tried the code but it still isn't working. (all of them) I'm attempting to put this in an `afterTextChanged()`, do you think this could be giving troubles? –  Jul 15 '14 at 18:01
  • Figured it out, had to add this `if (input.getText().toString().length() >= 1) { input.setCursorVisible(true); for (String s : list) { if (s.equals(input)) { Toast.makeText(getApplicationContext(), "Something", Toast.LENGTH_SHORT).show(); } } } –  Jul 15 '14 at 18:23
  • THANK YOU MAN! YOU ARE SUPER AWESOME! –  Jul 15 '14 at 18:29
  • Haha, any time! Glad I could help. – Akshay Jul 15 '14 at 19:00
2

Java's ArrayList.contains() uses the equals() method, which matches only exactly

How does a ArrayList's contains() method evaluate objects?

Community
  • 1
  • 1
MikeB
  • 2,402
  • 1
  • 15
  • 24
  • Are you suggesting use `myArrayList.equals(myWord)` because that does not work either :/ –  Jul 15 '14 at 17:26
  • 1
    I am not suggesting that. I am suggesting `myArrayList.contains(exactWordImLookingFor)` already looks for only exact matches – MikeB Jul 15 '14 at 17:56
1

For this case it would best if you use a HashSet. HashSet has method contains and should return result in O(1)

ksarmalkar
  • 1,884
  • 1
  • 16
  • 21
  • Could you post an example please? –  Jul 15 '14 at 17:45
  • I dont think example is needed .. in your case you can replace your arrayList by set (hashset) if ordering does not matter. Its all about ordering vs searching. check this http://java67.blogspot.com/2012/07/difference-between-arraylist-hashset-in-java.html – ksarmalkar Jul 15 '14 at 17:57
0

This isn't the most graceful answer, but could you iterate over the array and compare strings?

for (String s : arrayList)
    if (s.equals(value))
        // ...
JacksonHaenchen
  • 1,437
  • 1
  • 11
  • 16
0

Try this

EditText ed = (EditText) findViewById(R.id.ediText);
String temp  = ed.getText().toString();
boolean status = myArrayList.contains(temp);

or

EditText ed = (EditText) findViewById(R.id.ediText);
String s = ed.getText().toString();

for(String temp: myArrayList){
    if(temp.equals(s)){
       return true;
}
return false;

}

Thiago Souto
  • 761
  • 5
  • 13