Comparing a player's name to input

Discussion in 'Plugin Development' started by Gopaintman, Sep 19, 2013.

Thread Status:
Not open for further replies.
  1. Offline

    Gopaintman

    Hi,
    I need to get a player's name checked against a list and I have this so far:

    Code:java
    1. public String getPlayersFromStats(String name){
    2. name = name.toLowerCase();
    3. //get player name passed in from players array
    4. for(int i = 0; i<players.size(); i++){
    5. if (players.get(i).toLowerCase().contains(name)) {
    6. return name;
    7. }
    8. }
    9.  
    10. return null;
    11.  
    12.  
    13.  
    14.  

    We will assume that if it passes null it will be handled. "players" is an arraylist of player names already inputed from a file. I'm having the issue of passing in a name and having it returned. In theconfig the name is "Gopaintman", to this method I pass "Gopaintman" and I get a NPE. Any ideas?
     
  2. Offline

    chasechocolate

    Why not just use: if(players.contains(name))?
     
  3. Offline

    Appljuze

    You could try using another type of for loop and doing it this way...
    Code:java
    1. public String getPlayersFromStats(String name)
    2. {
    3. String nameToReturn;
    4. for(String s : players) // <-- your players arraylist
    5. {
    6. if(s.contains(name))
    7. nameToReturn = s;
    8. }
    9. return nameToReturn;
    10. }
     
  4. Offline

    Gopaintman

    The thing is that I'm not trying to get a player. I'm comparing input against the list to see if it works, to then allow it to do something else.
     
  5. Offline

    Janmm14

    Why you want to compare a string list with a string to get wheter they are in the list or not manually and don't want to use the contains() method?
    If you see problems with case: Just do playername.toLowerCase() and add this to the list.
     
Thread Status:
Not open for further replies.

Share This Page