简体   繁体   中英

NumberFormatException while converting a string to an integer value in java

When I'm trying to parse the input1 string to an integer value it's rising a NumberFormatException.I've tried replacing the spaces if any in the string but it didn't work for me.

int number = 0;
 String input1 = "12345354987"; 
 try{
   ip = Integer.parseInt(input1);

  }catch(NumberFormatException e){
        System.out.println("not a number");
    }
12345354987 > 2,147,483,64

Integer max range is 2,147,483,647 and you are crossing it. You may use Long or BigDecimal for big numbers.

Its because the Number in String is out of Integer range, If you really need this than use BigInteger for large values like this :

 String input1 = "12345354"; 
         BigInteger ib =new BigInteger(input1);
         System.out.println(ib);

or For small values that can fit in Long range you can use :

String input1 = "1234535455"; 
           Long ip = Long.parseLong(input1);
           System.out.println(ip);

您的数字大于整数最大值,请使用长然后尝试解析。

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