5

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)
Warrior
  • 3,184
  • 12
  • 44
  • 53

6 Answers6

6

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

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
5

I think you need to use BigInteger or BigDecimal

For example:

BigInteger bi;

String buyId  = "PSFT_20110328114728073793";
bi  = new BigInteger(buyId.replaceAll("PSFT_",""));

Add try-catch block too, with NumberFormatException

evilone
  • 22,410
  • 7
  • 80
  • 107
1

20110328094108069414 is out of the range of long.

RollingBoy
  • 2,767
  • 1
  • 14
  • 8
0

What about using something suiting for storing timestamps more than just a long? Date class? or JodaTime? Probably this post also will be helpful: Joda time : How to convert String to LocalDate?

Community
  • 1
  • 1
Alex Nikolaenkov
  • 2,505
  • 20
  • 27
0

I don't think the label PSFT_20110328114728073793 was ever intended to be used arithmetically. It seems to be a combination of type, date and ID. As others have pointed out 20110328114728073793 won't fit in a long. Maybe splitting the string into its original parts would make more sense for your use case ?

sthysel
  • 377
  • 3
  • 8
0

The number is out of Long range. Use BigInteger!

nknj
  • 2,436
  • 5
  • 31
  • 45