2017-02-14 19 views
-1

我有一個.dat文件,其中包含很多巨​​大的數字,例如568e24 1.20536e8 1.3526e12 1.5145e11。我已經將這些數字輸入爲String,並存儲在ArrayList中。我需要用這些數字來計算和表示太陽系中的引力體。但在java中它已經超過了Double和Int的上限。我想要在Java中使用這些數字。將大文本數字轉換爲BigInteger時NumberFormatException

這是我的代碼,它輸入這些數據並將這些數據作爲字符串存儲在ArrayList中。

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]); 

我想使用的BigInteger通過下面的代碼串改爲BigIntiger:

BigInteger reallyBig = new BigInteger("1898e24"); 
    System.out.print(reallyBig); 

但輸出是錯誤的:

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) 

所以......現在..什麼我應該如何使用這個龐大的數字,比如1898e24。我是否需要更改此號碼(1898e24)的格式,例如1898000000000 ...?

+2

BigDecimal是你的朋友 – Andremoniy

+2

['BigDecimal'](https://docs.oracle.com/javase/8/docs/api/java/math/BigDecimal.html)。 –

+0

你應該做什麼?在這裏傾銷你的問題之前用谷歌搜索。就那麼簡單。 – Paul

回答