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. Offline

    Pink__Slime

    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. Offline

    RainoBoy97

    Code:
    try{
      Integer.parseInt(args[0]);
    }catch(NumberFormatException e){
      //not and int :(
    }
    
     
    dark navi likes this.
  3. Offline

    willeb96

    PHP:
    public boolean isInt(String str) {
        try {
            
    Integer.parse(str);
            return 
    true;
        } catch (
    NumberFormatException e) {
            return 
    false;
        }
    }
     
    public 
    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. Offline

    finalblade1234

    willeb96
    when a player chats they chat as a string.
    like "1" is a String.. so whouldnt it return as a non-int?
     
  6. Offline

    skore87

    No, a string of "1" would parse perfectly fine under Integer.parse().
     
  7. Offline

    Cirno

    If args[0] is a single character, you can get the first character then use Character.isDigit() on it.
     
  8. Offline

    vYN

    This is how i do it:

    Code:
    String arg = args[1];
    if (Pattern.matches("[a-zA-Z]+", arg)) {
     
    }else{
     
    }
    Well I guess they still can use some weird signs... but ye.... It does the job for me...
     
  9. Offline

    willeb96

    My method returns a boolean value that you can use to see if a String is parsable or not =)
     
  10. Offline

    finalblade1234

    willeb96
    Oh okay, thats awesome!
    skore87
    I know that. thats not what i meant at all
     
  11. Offline

    Knaxel

    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

    Knaxel

  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

    Knaxel

    this is what i ment
    Code:
    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. Offline

    Pink__Slime

    I just tried using
    Code:
    try{
    Integer.parseInt(getConfig().getString("knight"));
    }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

    Tzeentchful

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

    or this if you want a numer with point something.
    Code:java
    1. getConfig().getDouble("key");
     
  19. Offline

    Pink__Slime

    Thanks.
     
  20. Offline

    kreashenz

    Then when you need to check if its a double, you would use
    Code:java
    1. Double.parseDouble(getConfig().getDouble("key"));

    or maybe a isDouble method, to check if it's a double.
    Code:java
    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