Wednesday, 24 January 2018

numberformatexception - Cannot convert String to Integer in Java





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?


Answer




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);

No comments:

Post a Comment

casting - Why wasn't Tobey Maguire in The Amazing Spider-Man? - Movies & TV

In the Spider-Man franchise, Tobey Maguire is an outstanding performer as a Spider-Man and also reprised his role in the sequels Spider-Man...