如何解析字符串中的大整数?

gur*_*gui 16 java

我有这样的方法:

Integer.parseInt(myInt)
Run Code Online (Sandbox Code Playgroud)

不是这个整数变得很长,我得到以下异常:

java.lang.NumberFormatException: For input string: "4000123012"
Run Code Online (Sandbox Code Playgroud)

我该怎么做才能避免这个错误并仍然保持功能运行?

我尝试使用BigInteger但没有parse方法,或者我没有找到它.

Alp*_*ati 23

像这样使用它.

 BigInteger number = new BigInteger(myInt);
Run Code Online (Sandbox Code Playgroud)


lop*_*ael 7

我的解决方案

String sLong = "4000123012";
long yourLong = Long.parseLong(sLong);
System.out.println("Long : "+yourLong);
Run Code Online (Sandbox Code Playgroud)

OutPut:

Long : 4000123012
Run Code Online (Sandbox Code Playgroud)

  • 打败了我!+1 (2认同)