Jump to content

getPlayerObjectFromName


Recommended Posts

Posted (edited)

I am trying to get a Player's object from the specified name. What I have currently only works when the name doesn't contain a space. What am I doing wrong?

    private Player getPlayerObjectFromName(String name) {
        List<Player> players = getPlayers().filter(player -> player != null && player.getName().equalsIgnoreCase(name));

        if (players != null) {
            for (Player p : players) {
                if (p.getName().equalsIgnoreCase(name)) {
                    return p;
                }
            }
        }
        return null;
    }

Edit: So I figured out the issue. Player objects use (char) 160 as a space which is why I was never getting a match when testing a name with (char) 32 as its space.

Edited by torquish
Posted
23 minutes ago, torquish said:

I am trying to get a Player's object from the specified name. What I have currently only works when the name doesn't contain a space. What am I doing wrong?


    private Player getPlayerObjectFromName(String name) {
      	return getPlayers().closest(player -> player.getName().replaceAll(“\\h”,  “).equalsIgnoreCase(name));
    }

Edit: So I figured out the issue. Player objects use (char) 160 as a space which is why I was never getting a match when testing a name with (char) 32 as its space.

Can’t check if that works as I’m on phone atm, but the idea is those weird characters should be in java’s horizontal whitespace character class, but not in the general whitespace class \s.

P.S. Replace the double quote characters, I don’t have that on iphone keyboard

  • Like 1
Posted
4 minutes ago, Token said:

Can’t check if that works as I’m on phone atm, but the idea is those weird characters should be in java’s horizontal whitespace character class, but not in the general whitespace class \s.

P.S. Replace the double quote characters, I don’t have that on iphone keyboard

Yes it works. Thanks.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...