[SOLVED]Howto: Check if value equals only one from an array?

Discussion in 'Plugin Development' started by Milkywayz, Apr 3, 2012.

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

    Milkywayz

    PHP:
            String[] skillnamess =
                {
    "ARCHERY""ACROBATICS""ALL""AXES""EXCAVATION",
                    
    "FISHING""HERBALISM""MINING""REPAIR""SWORDS""TAMING""UNARMED""WOODCUTTING"};
    This is my list of mcmmo skills.
    PHP:
    for (String st skillnamess) {
                        if(!
    e.getLine(1).equalsIgnoreCase(st)) {
                            
    e.getPlayer().sendMessage(ChatColor.RED "Line 2 contains invalid mcMMO Skillname");
                        break;
                        }
                    }
    This sends that error message each time that the line doesn't equal one of the skills. Obviously you can only fit and only make sense to have 1 skill for line 2. How could i do that?
     
  2. Offline

    Double0negative

    Code:JAVA
    1.  
    2. boolean flag = false;
    3. for (String st : skillnamess) {
    4. if(e.getLine(1).equalsIgnoreCase(st)) {
    5. flag = true;
    6. }
    7. }
    8. if(!flag){
    9. e.getPlayer().sendMessage(ChatColor.RED + "Line 2 contains invalid mcMMO Skillname");
    10. }
    11.  
     
    Milkywayz likes this.
  3. Offline

    Milkywayz

    I added a missing break; which fixed the spam, but it still sends the message because the string on line 2 cannot be all of the strings in the array.
     
  4. Offline

    Double0negative

    my code should fix that because if it equals one of them, dosent have to be all of them then it sets the flag to true and shouldnot display the message
     
  5. Offline

    Darkman2412

    Code:
    flag = skillnamess.contains(e.getLine(1).toUpperCase());
    // display message if !flag
    Isn't that the same?
     
  6. Offline

    Double0negative

    arrays dont have a contains method
     
  7. Offline

    Milkywayz

    Thanks double, this issue is solved
     
  8. Offline

    Darkman2412

    Double0negative
    Oh yeah, forgot about it :/

    Milkywayz
    You can also do this:
    Code:
    flag = Arrays.asList(skillnamess).contains(e.getline(1));
     
    monstuhs and Double0negative like this.
  9. Offline

    monstuhs

    +1 for Arrays.asList
     
Thread Status:
Not open for further replies.

Share This Page