deKeijzer
deKeijzer

Reputation: 510

(java) Can't check if ArrayList contains a specific String

There is this problem that i have been stuck with for hours. I'm trying to check an ArrayList for a specific String.

This is the ArrayList i am checking:

public ArrayList getAllowedPlayers() {
    return this.allowedPlayers;
}

The username's get added in lower case to this list.

Then this is what i use to check if the player typing the command is on the list:

 boolean addedToSSList = data.getAllowedPlayers().contains(astring[0].toLowerCase());

"data" refers to the ArrayList of the username that has been entered (astring[0]). Which is in another class. Then i use a print to chat message to check if the boolean turns true or false, which always returns false in my case.

        icommandsender.sendChatToPlayer("addedToList: "+addedToSSList);

However, i can see that the username entered is added to the list with:

        icommandsender.sendChatToPlayer("Allowed players: "+data.getAllowedPlayers())

The player from icommandsender is the person using the command to see if he is on the ArrayList.

So basically: I can see that the Username i am entering is on the ArrayList (in lowercase). The astring[0] is this username in lowercase that i entered. But my boolean method will still return false.

Upvotes: 0

Views: 291

Answers (1)

Rahul Tripathi
Rahul Tripathi

Reputation: 172538

Try this:

boolean addedToSSList = data.getAllowedPlayers().contains(astring[0].toLowerCase().trim());

May be you have some trailing spaces in your string.

EDIT:

public boolean findString(String s, ArrayList<String> al){
     for (String str : al){
        if (str.equalsIgnoreCase(s)){
            return true;
         }
     }
    return false;
  }

and then you can find like

findString("myString", yourArrayList);

Upvotes: 2

Related Questions