1

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

}

}

7
  • try with long Commented Jun 24, 2017 at 4:13
  • long also gives same exception Commented Jun 24, 2017 at 4:13
  • 1
    try Long.valueOf not Integer Commented Jun 24, 2017 at 4:15
  • int limit is till 2,147,483,647 Commented Jun 24, 2017 at 4:16
  • its working..... thanx Commented Jun 24, 2017 at 4:17

2 Answers 2

2

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"); 
Sign up to request clarification or add additional context in comments.

2 Comments

Error...java.lang.NumberFormatException: For input string: "9007199254"
@parimal Not left side, right side also you have to change.
1

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

try with long

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.