简体   繁体   中英

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.

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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM