Need help with if ARg

Discussion in 'Plugin Development' started by wesleydeman, May 11, 2012.

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

    wesleydeman

    Hello with this code.
    These ifs are called when i do /wesadvert arg0 arg1 etc....
    When i do /wesadvert.
    I go in the first if
    When i do /wesadvert cost.
    I go in the second if.
    But when i do /wesadvert cost test
    It won't go in the if(args[0].toLowerCase() == "cost")
    It will just run the end of the code this.
    sender.sendMessage("[WesAdvert] Ifs are done");

    Can you guys help me out?

    Code:
    if (args.length < 1) {
              sender.sendMessage("[WesAdvert] " + ChatColor.RED + "No sort specified!");
              return false;
            }
            if (args.length < 2) {
                sender.sendMessage("[WesAdvert] " + ChatColor.RED + "No arg specified!");
                return false;
            }
           
            if(args[0].toLowerCase() == "cost"){
                sender.sendMessage("[WesAdvert] Cost");
                return true;
            }
           
            if(args[0].toLowerCase() == "username"){
                sender.sendMessage("[WesAdvert] username");
                return true;
            }
           
            if(args[0].toLowerCase() == "amount"){
                sender.sendMessage("[WesAdvert] amount");
                return true;
            }
           
            if(args[0].toLowerCase() == "tag"){
                sender.sendMessage("[WesAdvert] tag");
                return true;
            }
            sender.sendMessage("[WesAdvert] Ifs are done");
     
  2. Offline

    r0306

    wesleydeman
    Always use .equalsIgnoreCase instead of == to check if strings match. == checks for the data values of the characters. Also, you should use else ifs to remove conflicts. As you can see, your args.length checks are going to prevent the latter part of your code from executing. You must also know that return exits the if checks and thus your final message "Ifs are done" will not execute so you must put it in each if check or remove it.

    Code:
     if (args.length == 1) {  //you should check the args length first to prevent nullpointerexceptions
            if(args[0].toLowerCase().equalsIgnoreCase("cost"){
                sender.sendMessage("[WesAdvert] Cost");
                return true;
            }
       
            if(args[0].toLowerCase().equalsIgnoreCase("username"){
                sender.sendMessage("[WesAdvert] username");
                return true;
            }
       
            if(args[0].toLowerCase().equalsIgnoreCase("amount"){
                sender.sendMessage("[WesAdvert] amount");
                return true;
            }
       
            if(args[0].toLowerCase().equalsIgnoreCase("tag"){
                sender.sendMessage("[WesAdvert] tag");
                return true;
            }
    }
          else if (args.length < 1) {
                sender.sendMessage("[WesAdvert] " + ChatColor.RED + "No arg specified!");
                return false;
            }
     
  3. You should remove toLowerCase() if you use equalsIgnoreCase() anyway.
    Also you don't need that else there but you do need to check if args.length is greater than the number you're using, don't use equal because that'll limit your options without realizing... so a basic subcommand would look like:

    Code:
    if(args.length > 0)
    {
        if(args[0].equalsIgnoreCase("arg"))
        {
             // ...
    
             return true;
        }
    
        // other commands
    }
    else
    {
        // no arguments
    }
     
  4. Digi I have a little thing to add to your code example: No need for a return true; at every subcommand:
    Code:java
    1.  
    2. if(args.length > 0)
    3. {
    4. if(args[0].equalsIgnoreCase("subcommand1"))
    5. {
    6. // ...
    7. }
    8. else if(args[0].equalsIgnoreCase("subcommand2"))
    9. {
    10. // ...
    11. }
    12. }
    13. else
    14. {
    15. // no arguments
    16. }
    17. return true;

    ;)
     
  5. Offline

    wesleydeman

    Thanks for the help guys should have noticed my mistake.......
     
Thread Status:
Not open for further replies.

Share This Page