Java 将大文本数字转换为BigInteger时出现NumberFormatException
我有一个.dat文件,其中包含许多巨大的数字,如568e24 1.20536e8 1.3526e12 1.5145e11。我已经将这些数字作为字符串输入,并存储在ArrayList中。我需要用这些数字来计算和表示太阳系中的引力体。但是在java中,它已经超过了Double和Int的上限。在java中使用这些数字我该怎么做呢 下面是我输入这些数据并将这些数据作为字符串存储在ArrayList中的代码Java 将大文本数字转换为BigInteger时出现NumberFormatException,java,algorithm,Java,Algorithm,我有一个.dat文件,其中包含许多巨大的数字,如568e24 1.20536e8 1.3526e12 1.5145e11。我已经将这些数字作为字符串输入,并存储在ArrayList中。我需要用这些数字来计算和表示太阳系中的引力体。但是在java中,它已经超过了Double和Int的上限。在java中使用这些数字我该怎么做呢 下面是我输入这些数据并将这些数据作为字符串存储在ArrayList中的代码 public static void main(String[] args) throws Exc
public static void main(String[] args) throws Exception{
Scanner input = new Scanner(new File("solarsystem.dat"));
ArrayList<String[]> BodyData = new ArrayList<String[]>();
input.nextLine();
while(input.hasNextLine()){
String x = input.nextLine();
String[] x1 = x.split("[^a-zA-Z0-9().]+");
BodyData.add(x1);
}
System.out.println(BodyData.get(0)[0]);
但结果是错误的:
Exception in thread "main" java.lang.NumberFormatException: For input string: "1898e24"
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.lang.Integer.parseInt(Integer.java:580)
at java.math.BigInteger.<init>(BigInteger.java:470)
at java.math.BigInteger.<init>(BigInteger.java:606)
at tryScanner.main(tryScanner.java:18)
线程“main”java.lang.NumberFormatException中的异常:对于输入字符串:“1898e24”
位于java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
在java.lang.Integer.parseInt(Integer.java:580)处
在java.math.biginger.(biginger.java:470)
在java.math.biginger.(biginger.java:606)
在tryScanner.main(tryScanner.java:18)
所以…现在。。我应该怎么做才能使用这个大数字,比如1898e24。我是否需要更改此数字(1898e24)的格式,例如18980000000…?您可以使用类似的内容。BigDecimal是您的朋友。您应该怎么做?在把你的问题扔到这里之前先搜索一下。简单的。BigDimple和BigInteger是你应该考虑的两种类型。它们都在“代码>双< /代码>的范围内。
Exception in thread "main" java.lang.NumberFormatException: For input string: "1898e24"
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.lang.Integer.parseInt(Integer.java:580)
at java.math.BigInteger.<init>(BigInteger.java:470)
at java.math.BigInteger.<init>(BigInteger.java:606)
at tryScanner.main(tryScanner.java:18)