我试图将String值转换为long,并得到: java.lang.NumberFormatException: For input string: "20110328094108069414"
我的代码:
String buyId = "PSFT_20110328114728073793";
long bookId = Long.parseLong(buyId .replaceAll("PSFT_",""));
Run Code Online (Sandbox Code Playgroud)
错误:
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)
Run Code Online (Sandbox Code Playgroud)
允许的最大值long是
9223372036854775807L
Run Code Online (Sandbox Code Playgroud)
你的价值是:
20110328094108069414L
Run Code Online (Sandbox Code Playgroud)
你不能用long这个.您可以使用BigInteger,但考虑到用例,我认为这String将是最合适的类型.(我无法想象您需要对book id进行整数运算,如果需要进行数值比较,您可以轻松实现自定义Comparator来对十进制字符串执行此操作.)
我认为你需要使用BigInteger或BigDecimal
例如:
BigInteger bi;
String buyId = "PSFT_20110328114728073793";
bi = new BigInteger(buyId.replaceAll("PSFT_",""));
Run Code Online (Sandbox Code Playgroud)
使用NumberFormatException添加try-catch块
| 归档时间: |
|
| 查看次数: |
18614 次 |
| 最近记录: |