0

I am facing the below problem:

I will be getting values similar or of greater length compared to temp value :

 public class NumberFormat {
     public static void main(String arg[]){
     Integer numValue = null;
     String temp="5474151538110135";
     numValue=Integer
    .parseInt(temp.trim());
     System.out.println("--> "+numValue);

}
}

Please provide a solution.

Exception in thread "main" java.lang.NumberFormatException: For input string: "5474151538110135"
    at java.lang.NumberFormatException.forInputString(NumberFormatException.java:60)
    at java.lang.Integer.parseInt(Integer.java:473)
    at java.lang.Integer.parseInt(Integer.java:511)
    at com.filetransfer.August.NumberFormat.main(NumberFormat.java:10)
1
  • 2
    the String is to big for an integer Commented May 29, 2014 at 11:23

3 Answers 3

5

5474151538110135 is greater than Integer.MAX_VALUE. Use Long.parseLong instead or BigInteger if the input number is likely to grow significantly

Long numValue = Long.parseLong(temp.trim());
Sign up to request clarification or add additional context in comments.

Comments

0

Probably beacuse the value is larger than max int value which is 2147483647.

System.out.println(Integer.MAX_VALUE);

You should parse it to Long which max value is 9223372036854775807.

System.out.println(Long.MAX_VALUE);

like this

Long numValue = null;
  String temp="5474151538110135";
  numValue=Long
      .parseLong(temp.trim());

Comments

0

I would recommend use BigInteger for avoiding errors

Advantage of BigInteger Class

Integer is a wrapper of the primitive type int.The wrapper classes are basically used in cases where you want to treat the primitive as an object for ex-trying to pass an int value in a method that would take only a type of Object in such a case you would want to wrap primitive int value in the wrapper Integer which is of type Object. To know specific advantages of Integer I would suggest you to take a look at the Integer api provided by Sun.

Now coming to the BigInteger, you would use it in calculations which deal with very large numbers.The use of BigIntegers is in Security where typically it is used for keys specifications.For more info on BigIntegers take a look at the following link http://docs.oracle.com/javase/7/docs/api/java/math/BigInteger.html

I hope the info helped you.

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.