java

How to check if you can create BigInteger from String


I have a method:

public BigInteger method(String param){
    try{
            return new BigInteger(param);
    }catch(NumberFormatException e){
        LOG.error(e, e);
    }
}

I want to check if I can create a BigInteger from those String param without generating NumberFormatException.

Is there any way do it this way?


Solution

  • Basically you have 2 possible approaches:

    1. You check if the String does only contain numbers, and not more than you could have in a Bigint

    2. cast in a try{}catch() and catch everything... this is not very performant and more a lazy method of doing it!

    Here you can probably find some inspiration :)