Solved Check if something is numeric

Discussion in 'Plugin Development' started by MrGeneralQ, Jun 1, 2016.

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

    MrGeneralQ

    Hi , i'm am currently looking for a simple way on how to detect if a string is numeric.
    I just can't figure it out how to do it. Wasn't able to find the correct answer though .

    I saw content saying use "if(isDouble(<string>)){

    }

    But that method doesn't even exist.

    Thanks!
     
  2. Online

    timtower Administrator Administrator Moderator

    @MrGeneralQ Integer.parseInt throws an error when it isn't an integer.
     
  3. Offline

    MrGeneralQ

    @timtower
    Could you give me a small example on how to use it? Thanks!
     
  4. Online

    timtower Administrator Administrator Moderator

    @MrGeneralQ Try catch statements returning true and false
     
  5. Offline

    MrGeneralQ

    Alright I'll give it a try.
     
  6. Offline

    I Al Istannen

    @MrGeneralQ
    Other way would be to get a NumberFormat instance and call the parse method. This way language specific seperators will be respected. E.g. in german "," is the decimal point and "." is used to group thousands. This will be respected, if you get the instance of the right language.
    You can also parse decimal numbers with it, to disable that call the setParseIntegerOnly to true.

    If parsing fails it throws a ParseException.
     
  7. Offline

    MrGeneralQ

    Thanks problem solved!
     
  8. Offline

    Batman500

    I use
    Code:
    boolean isNumber(String number) {
    if(number.matches("\\d+")) {return true;}
    else {return false;}
    }
    
    Edited
     
    Last edited: Jun 1, 2016
  9. Offline

    I Al Istannen

    @Batman500
    So "" is a number? and "+5" not? and "-5" naaah :p
     
    Last edited: Jun 1, 2016
  10. Offline

    Batman500

    Forgot it
    But args[] not use """
     
  11. Offline

    I Al Istannen

    @Batman500
    Might want to use "(+|-)?\\d+". It matches a bit more and a bit more accurately.
     
Thread Status:
Not open for further replies.

Share This Page