Search code examples
javablackberrynumberformatexception

Getting A NumberFormat Exception in BlackBerry


I am trying to parse a string to int value. But i am getting a NumberFormat Exception. I am writing the below code:

     Logger.out("Myprof", "Contact "+strContact);
    try{
        i = Integer.parseInt(strContact.trim());
        Logger.out("Myprof", "Contact8686866      "+i);
    }
    catch(Exception e)
    {
        Logger.out("Myprof", "exce "+e.toString());
    }

Now when i am passing like below:

i = Integer.parseInt("11223344");

I am getting the i value as 11223344.

Where i am doing wrong here? Please Help.


Solution

  • The input value of 9875566521 is greater than Integer.MAX_VALUE of 2147483647. Instead use a Long. (BigInteger not an option for Blackberry)

    Long number = Long.parseLong(strContact);
    Logger.out("Myprof", "Contact8686866 " + number);
    

    If the intended input numbers are greater then Long.MAX_VALUE, then Character.iDigit can be used as an alternative to validate values:

    private static boolean isValidNumber(String strContact) {
       for (int i = 0; i < strContact.length(); i++) {
          if (!Character.isDigit(strContact.charAt(i))) {
             return false;
          }
       }
    
       return true;
    }