parimal
parimal

Reputation: 31

Getting Number Format Exception while converting String to Integer

I am writing a code for converting a number given in string to integer. But it gives Number Format Exception for more than 9 digit number. Is there any other way for doing this.

public class StringToInt {

public static void main(String args[])
{
    try
    {
        long test =Integer.valueOf("9007199254");   
        System.out.println("num :"+test); 
    }catch(NumberFormatException e)
    {
        System.out.println("Error..."+e.toString());
    }

}

}

Upvotes: 1

Views: 551

Answers (2)

Jaydeep Devda
Jaydeep Devda

Reputation: 735

int limit is 2,147,483,647 thats why it gives NumberFormatException

try with long

long test =Long.valueOf("9007199254"); 

Upvotes: 1

Suresh Atta
Suresh Atta

Reputation: 121998

But it gives Number Format Exception for more than 9 digit number.

Consider using long type as your number is greater than integer max range (2,147,483,647).

long test =Long.valueOf("9007199254"); 

Upvotes: 2

Related Questions