Solved Checking if an argument is an int

Discussion in 'Plugin Development' started by Debels, Dec 10, 2012.

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

    Debels

    So I want to check if an argument is an int, specifically args[0]

    So while using google to see if I could find an answer I encountered the following options, but I want to know if there is another way:

    -Getting the numbers out of the string.
    or
    -Integer.parseInt(args[0]);

    Both methods I already knew, but both methods return errors, which I don't want to, I know I been asking for to much help, but I want to learn all that I can this week :)

    Thanks for reading,

    Debels.
     
  2. Offline

    Scizzr

    Code:
    public static boolean isInt(String s) {
        try {
            Integer.parseInt(s);
        } catch (NumberFormatException nfe) {
            return false;
        }
        return true;
    }
    
    Sample use:
    Code:
    public boolean onCommand(CommandSender s, Command cmd, String lbl, String[] args) {
        if (args.length >= 1) {
            if (isInt(args[0])) {
                int num = Integer.parseInt(args[0]);
                //do something with the num variable
            }
        }
        return true;
    }
    
     
    2008Choco, ZeusAllMighty11 and Debels like this.
  3. Offline

    Debels

    Thanks it works perfectly :D
     
Thread Status:
Not open for further replies.

Share This Page