1

I am doing simple conversion from string to int but getting number format exception :

I have use below java Program :

String cId = "7000000141";
int iCid = Integer.parseInt(cId);
System.out.println(iCid);

Getting below exception :

Exception in thread "main" java.lang.NumberFormatException: For input string: "7000000141"
    at java.lang.NumberFormatException.forInputString(NumberFormatException.java:48)
    at java.lang.Integer.parseInt(Integer.java:459)
    at java.lang.Integer.parseInt(Integer.java:497)

Why I am getting the above exception?

Shiladittya Chakraborty
  • 4,270
  • 8
  • 45
  • 94

1 Answers1

1

That's because it's out of range of integer. The maximum allowed value for integer is 2147483647

In Java, following are the minimum and maximum values.

        width                     minimum                         maximum
int:   32 bit              -2 147 483 648                  +2 147 483 647
long:  64 bit  -9 223 372 036 854 775 808      +9 223 372 036 854 775 807

source


Use a 'long' datatype instead

public class HelloWorld
{
  public static void main(String[] args)
  {
    String cId = "7000000141";
    long iCid = Long.parseLong(cId);
    System.out.println(iCid);
  }
}
Community
  • 1
  • 1
Raman Sahasi
  • 30,180
  • 9
  • 58
  • 71