0

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.

2
  • Maybe you have leading/trailing spaces that you don't see. Commented Oct 19, 2015 at 13:07
  • do this astring[0].toLowerCase().trim() Commented Oct 19, 2015 at 13:08

1 Answer 1

2

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);
Sign up to request clarification or add additional context in comments.

3 Comments

The command string always is "username" without spaces so this wont change anything. But thanks for your answer.
@user3482325:- Updated my answer. Please check now!
Got it working, thank you! Main problem however was that my astring[0] was the username entered, not the players username that is typing the command. Oops.

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.