Parsing an integer from an argument

Discussion in 'Plugin Development' started by HamOmlet, Jun 12, 2012.

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

    HamOmlet

    Pretty straightforward: I'm trying to check if an argument is either 1 or 2. Would the code below work?
    Code:
            if(args.length == 1){
                if(Integer.parseInt(args[1]) != 1 || Integer.parseInt(args[1]) != 2) {
                    player.sendMessage(ChatColor.RED + "You may only use '1' or '2' as an argument!");
                    return true;
                }
                SettingsManager.getInstance().setDeathmatch(game, Integer.parseInt(args[1]), l.toVector());
                player.sendMessage(ChatColor.GREEN + "Deathmatch " + Integer.parseInt(args[1]) + " location set!");
                return true;
            } else {
                player.sendMessage(ChatColor.RED + "You must use '1' or '2' as an argument!");
                return true;
            }
     
  2. Offline

    r0306

    HamOmlet
    Since your argument length is only 1, use an index of 0 instead.
    Code:
    if(Integer.parseInt(args[0]) != 1 || Integer.parseInt(args[0]) != 2) {
    
    Otherwise, yes it will work after you change the args.
     
  3. Offline

    HamOmlet

    I have the horrible habit of not counting 0 as part of an argument - thanks!

    EDIT: Just to be sure I understand this, if the command were like so:

    /pvp setdeathmatch 1|2

    The 1|2 are considered the first argument, which is 0. However, I specified that the argument length much be at least 1, which confuses me. Would you mind clearing this up for me?
     
  4. Offline

    r0306

  5. Offline

    HamOmlet

    Sorry - edited my post above.
     
  6. Offline

    r0306

    "pvp" is your command, which doesn't count as an argument. Then setdeathmatch 1/2 are your arguments. So you would have an args length of 2 which makes 1/2 your args[1].

    EDIT: In this case, you would also need to check if args[0] is "setdeathmatch".

    Code:
            if(args.length == 2){
                if(Integer.parseInt(args[1]) != 1 || Integer.parseInt(args[1]) != 2) {
                    player.sendMessage(ChatColor.RED + "You may only use '1' or '2' as an argument!");
                    return true;
                }
                SettingsManager.getInstance().setDeathmatch(game, Integer.parseInt(args[1]), l.toVector());
                player.sendMessage(ChatColor.GREEN + "Deathmatch " + Integer.parseInt(args[1]) + " location set!");
                return true;
            } else {
                player.sendMessage(ChatColor.RED + "You must use '1' or '2' as an argument!");
                return true;
            }
     
  7. Offline

    krinsdeath

    If you want to be safe and not throw uncaught exceptions, you need to catch NumberFormatException.

    Code:
    try {
      int param1 = Integer.parseInt(args[0]);
      int param2 = Integer.parseInt(args[1]);
      ... do stuff 
    } catch (NumberFormatException e) {
      getLogger().warning("Expected number! " + e.getMessage());
    } catch (ArrayIndexOutOfBoundsException e) {
      getLogger().warning("Invalid parameter count! " + e.getMessage());
    }
    
     
  8. Offline

    HamOmlet

    Thank you both for your replies! I really appreciate it. ;)

    EDIT: Here's the finished code:

    Code:
            if(args.length == 2){
                if(Integer.parseInt(args[1]) != 1 || Integer.parseInt(args[1]) != 2) {
                    player.sendMessage(ChatColor.RED + "You may only use '1' or '2' as an argument!");
                    return true;
                }           
                try{
                    int param1 = Integer.parseInt(args[1]);
                    SettingsManager.getInstance().setDeathmatch(game, param1, l.toVector());
                    player.sendMessage(ChatColor.GREEN + "Deathmatch " + param1 + " location set!");
                    return true;
                } catch (NumberFormatException e) {
                    player.sendMessage(ChatColor.RED + "You must use either '1' or '2' as your argument!");
                } catch (ArrayIndexOutOfBoundsException e) {
                    player.sendMessage(ChatColor.RED + "You must use either '1' or '2' as your argument!");
                }
            } else {
                player.sendMessage(ChatColor.RED + "You must use '1' or '2' as an argument!");
                return true;
            }
            return true;
        }
     
  9. Offline

    desht

    You're still making calls to Integer.parseInt() outside the try block. Also, you're already checking args.length, so catching ArrayIndexOutOfBoundsException is redundant there. Finally, when you're retyping the same error message three times, it's a hint that a single method would be better.

    I'd suggest something like:
    PHP:
      try {
        
    int param1 Integer.parseInt(args[1]);
        if (
    param1 == || param1 == 2) {
          
    SettingsManager.getInstance().setDeathmatch(gameparam1l.toVector());
          
    player.sendMessage(ChatColor.GREEN "Deathmatch " param1 " location set!");
        } else {
          
    showUsage(player);
        }
      } catch (
    NumberFormatException e) {
        
    showUsage(player);
      } catch (
    ArrayIndexOutOfBoundsException e) {
        
    showUsage(player);
      }
      return 
    true;
     
     
    private 
    void showUsage(Player player) {
      
    player.sendMessage(ChatColor.RED "You must use '1' or '2' as an argument!");
    }
     
Thread Status:
Not open for further replies.

Share This Page