Better way to check player args[0] int

Discussion in 'Spigot Plugin Development' started by MadMaxCookie, May 23, 2016.

  1. Code (Java):
    return title;
    which is the better way to check player's args[0] is Integer ?

    Code (Java):
    String s = args[0];
    // this way
    int arg0 = Integer.parseInt(s);
    Code (Java):
    // or this way
    int i;
    try {
      i = args[0]
     }(catch (NumberException ex) {
    // is there another better way ?
    // or I've done it wrong ?
  2. Make a try block, and make an integer variable equal to Integer.parseInt(args[0]). Then add a NumberFormatException catch block (if its not a number)

    Sent from my SGH-I747M using Tapatalk
  3. I personally use isInt (from my utils) and integer.parseInt if it doesnt return false, have a look at an example:

    Code (Text):
    int number;
    if(!isInt(args[0]))  {
       // Not an integer value
    } else {
       // It's an integer value
       number = Integer.parseInt(args[0]);
        public static boolean isInt(String s) {
            try {
            } catch (NumberFormatException nfe) {
                return false;
            return true;
    Of course you can also just do the isInt method manually each time but i prefer this way of doing it.
    • Informative Informative x 1
  4. Regex:
  5. I thought the regex was \\d+

    Sent from my SGH-I747M using Tapatalk
  6. nop, I just checked another source to make sure I didn't post incorrect information.

    Here's a snippet for checking for alpha-numeric using Regex.

    Code (Text):
    public boolean isAlphaNumeric(String s){
        String pattern= "^[a-zA-Z0-9]*$";
                return true;
            return false;  
  7. :/ \\d+ works when I use it, maybe there are multiple patterns?

    Sent from my SGH-I747M using Tapatalk
  8. O gawd yes, there are TONS to ways to use regex.

    I mean the /d makes sense because that's close to how ints are
    displayed in C using Printf. ( %d > /d) so yeah, close.
  9. The standard way would be "\\d+", not "[0-9]+", because it's easier for people to read. However, \d in regex stands for digit and %d stands for decimal in C. %i is also usable in printf. The more you know :p
  10. MiniDigger


    using regex to check args, great idea for performance...
    just try parsing it as a int (or double) and catch the numberformatexception.
    But don't do it the way @danablend did it because that would force you to parse the int two times which would cause double the performance impact.
    • Agree Agree x 1
  11. Thanks for help got it right. so I have to use both ways my bad.
  12. You should not be using regular expressions to check if a string is a number because there are many different ways to define a number in Java, on such example is hexadecimal, meaning it can have letters.

    Another valid number is 1.037e9 which also wouldn't pass your regular expression.

    As well you should only use try catch when it is either impossible to predict or account for all user input.

    Here is a method that will check if your input string is a valid number (includes all my above mentions and decimals):
    Code (Java):
    public static boolean isInt(String value) {
        NumberFormat formatter = NumberFormat.getInstance();
        ParsePosition position = new ParsePosition(0);
        formatter.parse(value, position);
        return value.length() == position.getIndex();
    I use this in my core api, will never throw an exception.
  13. MiniDigger


    but this will force you to parse the string twice. I realy don't see the problem with catching that NumberFormatException that Interger#parseInt can throw.
  14. Because parsing an identical string more than once in the same way is not detrimental, but creating, filling and throwing a stack trace (which happens when you fail to parse that string as an integer) definitely is.

    You can even separate the number formatted to grab the specific number type after it checks the length against the string value.
  15. if you really worry about the performance, you need to check how java compiler and JIT/AOT compiler does their optimisation.
  16. MiniDigger


    I don't worry that much about performance. Parsing the same string twice just seemed wrong to me.
  17. i understand your point but many modern compiler's optimisation capabilities make lot of conventional source-code level optimisation rather useless/meaningless.
  18. Why make life so complicated.

    The first example will throw an exception if it's not an integer, the second will, but you can catch it.. Use the later. If Java was that worried about the performance of Integer.parseInt, they'd have fixed it.
  19. Regex is about as bad as throwing Exceptions, so the added complexity is making it a worse solution.

    Try-catching twice is doing twice the work, and regardless how often you scream JIT, it still doesn't cover for your incapability of just parsing once (and for those who want to not repeat it everywhere, return an OptionalInt)

    Nice solution, but overly complex (much slower than Integer.parseInt with valid inputs).

    As for hexadecimals and scientific notation, that likely falls out of the given domain scope - have you ever seen plugins use them, or are they user friendly? Guess the answer to both is 'no'.

    As for incorrect values and thrown exceptions, you should most definitely show me some benchmarks with mixed values (both correct integers and invalid entries), because I highly doubt you'll find results favouring your approach.
    • Agree Agree x 1
  20. Using Regex was just a suggestion, ya'll were looking for a way to grab numerical information from the String,
    I wasn't taking code efficiency into account.