Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to parse a big integer from a string? [duplicate]

Tags:

java

I have a method like this:

Integer.parseInt(myInt)

not this integer becomes to long and I'm getting the following exception:

java.lang.NumberFormatException: For input string: "4000123012"

what can I do to avoid this error and still keep alle the functions runnning?

I tried to use BigInteger but there is no parse method, or I did not found it.

like image 375
gurehbgui Avatar asked May 24 '13 08:05

gurehbgui


People also ask

How do you parse a BigInteger?

Firstly, take two BigInteger objects and set values. BigInteger one, two; one = new BigInteger("99"); two = new BigInteger("978"); Now, parse BigInteger object “two” into Binary.

What is parse int method?

The parseInt method parses a value as a string and returns the first integer. A radix parameter specifies the number system to use: 2 = binary, 8 = octal, 10 = decimal, 16 = hexadecimal. If radix is omitted, JavaScript assumes radix 10. If the value begins with "0x", JavaScript assumes radix 16.


3 Answers

Use it like this.

 BigInteger number = new BigInteger(myInt);
like image 77
Alpesh Prajapati Avatar answered Sep 23 '22 22:09

Alpesh Prajapati


My solution :

String sLong = "4000123012";
long yourLong = Long.parseLong(sLong);
System.out.println("Long : "+yourLong);

OutPut :

Long : 4000123012
like image 20
lopez.mikhael Avatar answered Sep 20 '22 22:09

lopez.mikhael


You can use Long.parse for integers too:

Long.parseLong(myInt)

which of course returns a long.

like image 41
davek Avatar answered Sep 21 '22 22:09

davek