1

i Am beginner of java. pls help me,

int i = Integer.parseInt("9876543210123456");
System.out.println("Integer: " + i);

i got below error,

java.lang.NumberFormatException for input string : "9876543210123456"
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Kannan Arumugam
  • 1,119
  • 2
  • 18
  • 27

7 Answers7

3

It's crossing the integer limit , You need BigInteger

BigInteger b = new BigInteger("9876543210123456");
Suresh Atta
  • 120,458
  • 37
  • 198
  • 307
2

You can't convert the above value to an int, as it exceeds Integer's max value: 2,147,483,647.

Instead try:

Long.parseLong("9876543210123456");

Long has a maximum value of 9,223,372,036,854,775,807.

To check the maximum values of these and other primitive data types try:

Integer.MAX_VALUE;
Long.MAX_VALUE;
// etc for Float, Double...
// Also try MIN_VALUE
Taylor Hx
  • 2,815
  • 23
  • 36
2

In Java, the primitive int data type is a 32-bit signed two's complement integer http://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html

You can use Long:

long i = Long.parseLong("9876543210123456");
System.out.println("Long: " + i);

Refer to the API here - http://docs.oracle.com/javase/7/docs/api/java/lang/Long.html

Or you can also use BigInteger:

BigInteger i = new BigInteger("9876543210123456");
System.out.println("BigInteger: " + i);

Refer to the API here - http://docs.oracle.com/javase/7/docs/api/java/math/BigInteger.html

BigInteger is analogous to the primitive integer types except that it provides arbitrary precision, hence operations on BigIntegers do not overflow or lose precision. In addition to standard arithmetic operations, BigInteger provides modular arithmetic, GCD calculation, primality testing, prime generation, bit manipulation, and a few other miscellaneous operations.

If you are confused whether to use a primitive or an object reference, here is a good discussion

When to use primitive and when reference types in Java

Community
  • 1
  • 1
Indu Devanath
  • 2,068
  • 1
  • 16
  • 17
0

In java range of int is from -2,147,483,648 to 2,147,483,647. So try long instead of int.

Vimal Bera
  • 10,346
  • 4
  • 25
  • 47
0
NumberFormat nf = NumberFormat.getInstance();

You can also use a NumberFormat to parse numbers:

 myNumber = nf.parse("9876543210123456");

see below

http://docs.oracle.com/javase/7/docs/api/java/text/NumberFormat.html

Deepak
  • 2,287
  • 1
  • 23
  • 30
0

Although BigInteger might work, but the best way is to keep it String only and display it as String. That's the ideal way for dealing with long digit numbers and you won't end up with any type of exceptions.

roger_that
  • 9,493
  • 18
  • 66
  • 102
-1
long x = Long.parseLong("9876543210123456");

System.out.println("Integer: " + x);

or else ple use follow line

BigInteger bint = new BigInteger("9999999999999999", 16);

Please try this..Can't avle to convert the more than 13 digits string to int..so,you should convert to long

Arun Kumar
  • 100
  • 1
  • 2
  • 12