Solved Checking if args is an int

Discussion in 'Plugin Development' started by Pink__Slime, Apr 26, 2013.

Thread Status:
Not open for further replies.
  1. Is there an easy way of checking if a command argument is an integer?

    Small and simple question, hoping to get this answered asap.

    Thanks. :)
  2. Code:
    }catch(NumberFormatException e){
      //not and int :(
    dark navi likes this.
  3. PHP:
    public boolean isInt(String str) {
        try {
        } catch (
    NumberFormatException e) {
    boolean onCommand() {
        if (
    isInt(args[0])) {
    // The first argument is an int
    } else {
    // The first argument is NOT an int
  4. Offline

    Sagacious_Zed Bukkit Docs

    Also remember to check if args0 exists.
  5. willeb96
    when a player chats they chat as a string.
    like "1" is a String.. so whouldnt it return as a non-int?
  6. No, a string of "1" would parse perfectly fine under Integer.parse().
  7. If args[0] is a single character, you can get the first character then use Character.isDigit() on it.
  8. This is how i do it:

    String arg = args[1];
    if (Pattern.matches("[a-zA-Z]+", arg)) {
    Well I guess they still can use some weird signs... but ye.... It does the job for me...
  9. My method returns a boolean value that you can use to see if a String is parsable or not =)
  10. willeb96
    Oh okay, thats awesome!
    I know that. thats not what i meant at all
  11. Offline


    if(args[whatever number] == a list of numebr options like "1" + "2" + "4" + "5"
  12. Offline

    Sagacious_Zed Bukkit Docs

    You cannot handle all possible strings with numeric characters in it this way.
  13. Offline


  14. Offline

    Sagacious_Zed Bukkit Docs

    You can't represent an infinite set of possible numbers as strings with a finite set of 'or statements'. And no, you can't have an infinite set of or statements.
  15. Offline


    this is what i ment
    if(args[0] == "1"){
    if(args[0] == "2"){
  16. Offline

    Sagacious_Zed Bukkit Docs

    And "3" is already not on that list. For any number that you have listed here, I can always find one that is not.
  17. I just tried using
    }catch(NumberFormatException e){
    //not an int
    but it doesn't seem to be what I'm looking for.

    I have values in my config (e.g. example: 6) and I need to use if(econ.getBalance(player.getName()) == getConfig().getString("example"))

    I always get "Incompatible operand double and String"
  18. Offline


    Your trying to compare a string to a double. use
    1. getConfig().getInt("key");

    or this if you want a numer with point something.
    1. getConfig().getDouble("key");
  19. Thanks.
  20. Then when you need to check if its a double, you would use
    1. Double.parseDouble(getConfig().getDouble("key"));

    or maybe a isDouble method, to check if it's a double.
    1. public static boolean isDouble(String string) {
    2. try {
    3. Double.parseDouble(string);
    4. } catch (NumberFormatException e) {
    5. return false;
    6. }
    7. return true;
    8. }
Thread Status:
Not open for further replies.

Share This Page