java.lang.NumberFormatException:对于输入字符串:“20110328094108069414”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5509331/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 11:28:05  来源:igfitidea点击:

java.lang.NumberFormatException: For input string: "20110328094108069414"

javaexception

提问by Warrior

Am trying to convert a String value to long, and am getting : java.lang.NumberFormatException: For input string: "20110328094108069414"

我正在尝试将 String 值转换为 long,并且得到: 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)

回答by Stephen C

The largest allowed longis

允许的最大long

  9223372036854775807L

and your value is:

你的价值是:

  20110328094108069414L

You can't use a longfor this. You could use BigIntegerinstead, but given the use-case, I think that Stringwould be the most appropriate type. (I can't imagine you needingto 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.)

您不能long为此使用 a 。您可以BigInteger改用,但考虑到用例,我认为这String将是最合适的类型。(我无法想象您需要对书籍 ID 进行整数运算,如果您需要进行数字比较,您可以轻松实现自定义 Comparator 来对十进制字符串执行此操作。)

回答by evilone

I think you need to use BigIntegeror BigDecimal

我认为您需要使用BigIntegerBigDecimal

For example:

例如:

BigInteger bi;

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

Add try-catch block too, with NumberFormatException

也添加 try-catch 块,使用 NumberFormatException

回答by RollingBoy

20110328094108069414 is out of the range of long.

20110328094108069414 超出了 long 的范围。

回答by Alex Nikolaenkov

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?

使用适合存储时间戳而不只是很长的东西怎么样?约会课?还是JodaTime?可能这篇文章也会有帮助:Joda time : How to convert String to LocalDate?

回答by sthysel

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 ?

我认为标签 PSFT_20110328114728073793 从来没有打算在算术上使用。它似乎是类型、日期和 ID 的组合。正如其他人指出的那样 20110328114728073793 不适合长时间使用。也许将字符串拆分为其原始部分对您的用例更有意义?

回答by nknj

The number is out of Long range. Use BigInteger!

该号码超出了远程范围。使用大整数!