Am trying to convert a String value to long, and am getting : java.lang.NumberFormatException: For input string: "20110328094108069414"
My code :
String buyId = "PSFT_20110328114728073793";
long bookId = Long.parseLong(buyId .replaceAll("PSFT_",""));
Error:
10:12:10,522 ERROR [STDERR] java.lang.NumberFormatException: For input string: "20110328094108069414"
10:12:10,522 ERROR [STDERR] at java.lang.NumberFormatException.forInputString(NumberFormatException.java:48)
10:12:10,522 ERROR [STDERR] at java.lang.Long.parseLong(Long.java:415)
10:12:10,522 ERROR [STDERR] at java.lang.Long.parseLong(Long.java:461)
10:12:10,522 ERROR [STDERR] at unilog.com.user.ejb.userDAOORCL.checkCWSUserReg(userDAOORCL.java:363)
10:12:10,522 ERROR [STDERR] at unilog.com.user.ejb.userEJBBean.checkCWSUserReg(userEJBBean.java:141)
10:12:10,522 ERROR [STDERR] at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
10:12:10,523 ERROR [STDERR] at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
10:12:10,523 ERROR [STDERR] at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
10:12:10,523 ERROR [STDERR] at java.lang.reflect.Method.invoke(Method.java:585)
10:12:10,523 ERROR [STDERR] at org.jboss.invocation.Invocation.performCall(Invocation.java:359)
10:12:10,523 ERROR [STDERR] at org.jboss.ejb.StatelessSessionContainer$ContainerInterceptor.invoke(StatelessSessionContainer.java:237)
10:12:10,523 ERROR [STDERR] at org.jboss.resource.connectionmanager.CachedConnectionInterceptor.invoke(CachedConnectionInterceptor.java:158)
The NumberFormatException is an unchecked exception in Java that occurs when an attempt is made to convert a string with an incorrect format to a numeric value. Therefore, this exception is thrown when it is not possible to convert a string to a numeric type (e.g. int, float).
The NumberFormatException occurs when an attempt is made to convert a string with improper format into a numeric value. That means, when it is not possible to convert a string in any numeric type (float, int, etc), this exception is thrown. It is a Runtime Exception (Unchecked Exception) in Java.
NumberFormatException: For input string: "null" is specifically saying that the String you receive for parsing is not numeric and it's true, "null" is not numeric. Many Java methods which convert String to numeric type like Integer. parseInt() which convert String to int, Double.
If you provide the input string like "1.0" and you try to convert this string into an integer value, it will throw a NumberFormatException exception. Example- Integer. parseInt("1.. 0");
The largest allowed long
is
9223372036854775807L
and your value is:
20110328094108069414L
You can't use a long
for this. You could use BigInteger
instead, but given the use-case, I think that String
would be the most appropriate type. (I can't imagine you needing to do integer arithmetic on book ids, and if you need to do numeric comparison, you could easily implement a custom Comparator to do that on decimal strings.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With