Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot convert String to Integer in Java [duplicate]

I have written a function to convert string to integer

   if ( data != null )
   {
        int theValue = Integer.parseInt( data.trim(), 16 );
        return theValue;
   }
   else
       return null;

I have a string which is 6042076399 and it gave me errors:

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

Is this not the correct way to convert string to integer?

like image 423
Ding Avatar asked Aug 18 '10 23:08

Ding


2 Answers

Here's the way I prefer to do it:

Edit (08/04/2015):

As noted in the comment below, this is actually better done like this:

String numStr = "123";
int num = Integer.parseInt(numStr);
like image 107
Steve Pierce Avatar answered Sep 30 '22 06:09

Steve Pierce


An Integer can't hold that value. 6042076399 (413424640921 in decimal) is greater than 2147483647, the maximum an integer can hold.

Try using Long.parseLong.

like image 45
Borealid Avatar answered Sep 30 '22 07:09

Borealid