0

i have an arraylist and I want to check whether the arraylist have a certain string. I found that .contains() can do the trick. But when i run it in a loop to check the word "bot" in an arraylist. The results also include "chatbot" and "robot" as "bot" which is not suppose to be the result that I want. But if I do it without the loop, it work just fine which I dont understand why.

The code:

// Java code to demonstrate the working of 
// contains() method in ArrayList of string 

// for ArrayList functions 
import java.util.ArrayList; 

public class test { 
    public static void main(String[] args) 
    { 

        // creating an Empty String ArrayList 
        ArrayList<String> arr = new ArrayList<String>(4); 
        ArrayList<String> arr2 = new ArrayList<String>(4);

        // using add() to initialize values 
        arr.add("chatbot"); 
        arr.add("robot"); 
        arr.add("bot"); 
        arr.add("lala"); 

        // use contains() to check if the element 
        for (int i=0;i<arr.size();i++){
            boolean ans = arr.get(i).contains("bot"); 
            if (ans) {System.out.println("1: The list contains bot"); }
            else
                {System.out.println("1: The list does not contains bot");}
        }

        System.out.println();

        for (String str : arr) {
        if (str.toLowerCase().contains("bot")) {
            System.out.println("2: The list contains bot");;
        }
        else
            {System.out.println("2: The list does not contains bot");}
        }


        // use contains() to check if the element 
        System.out.println();
        arr2.add("robot");
        boolean ans = arr2.contains("bot"); 

        if (ans) 
            System.out.println("3: The list contains bot"); 
        else
            System.out.println("3: The list does not contains bot"); 
    } 
} 

The result:

1: The list contains bot
1: The list contains bot
1: The list contains bot
1: The list does not contains bot

2: The list contains bot
2: The list contains bot
2: The list contains bot
2: The list does not contains bot

3: The list does not contains bot
2
  • In the first two loops, you're looping through the list and checking each string in the list to see if it contains the word "bot" as a substring. For the final if statement, you're checking to see if the list arr2 contains an element that is the exact String "bot" (i.e., you're not checking for substrings). Since it has "robot", but doesn't have "bot", it returns false. Commented Feb 3, 2020 at 21:12
  • ohh i see.. so is there a way that i want to check the substring to be exactly "bot" and not "chatbot" or "robot" in an arraylist? Commented Feb 3, 2020 at 21:19

2 Answers 2

3

You are basically checking if arraylist arr2 contains the word 'bot' which it doesn't. You must check if the first element contains the word. arr2[0].contains("bot")

Sign up to request clarification or add additional context in comments.

1 Comment

ohhh yeah u r right..well basically after i change it to be arr2.get(0).contains("bot"), it returns true.. :")
2

Use .equals instead of .contains if you want to match only the exact string:

public static void main(String s[]) {
        test.add("bot");
        test.add("ibot");
        test.add("abot");
        String str = "bot";

        for(int i=0;i<test.size();i++) {
            if(str.equals(test.get(i))) {
                System.out.println("True");
            }
            else {
                System.out.println("False");
            }
        }
    }

Comments

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.