Checking if args[0] is a double or int; Converting a double to int

Discussion in 'Plugin Development' started by Dysko, Nov 29, 2014.

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

    Dysko

    I'm having trouble figuring out if args[0] in a command is a double or an int, and if it is a double I want to convert it to an int and then do something else with said int.

    Any help?
     
  2. Dysko args[0] is the /command this and if you want to turn a double into an int then do int i = Integer.valueOf(double);
     
  3. Don't you mean Integer.parseInt(string)? Hence the string instead of double. Also wrap it around a try-catch block.
     
  4. Offline

    AoH_Ruthless

    bwfcwalshy
    That wouldn't work; I think OP is asking how to check if the argument itself is a double or an int.

    Dysko
    In a try-catch block (or a separate method that handles the try/catch for you), you can use Double.parseDouble(String) or Integer.parseInt(String) to check for a double or int, respectively. The try-catch block will handle the resultant NumberFormatException if the argument is not a double or an int.
     
  5. Offline

    Dysko

    Assist
    bwfcwalshy
    So I would make args[0] parseInt no matter what? The way this command is being ran there is 0 chance of it not being a double or int.

    And for parseInt, if it is a double, would it just round the double to an int?
     
  6. Offline

    AoH_Ruthless

    Dysko
    You will receive an error when parsing a double as an integer; doubles are not integers.

    Read my previous post for more info.
     
Thread Status:
Not open for further replies.

Share This Page