AAMA
AAMA

Reputation: 115

NumberFormatException while converting large textual numbers to BigInteger

I have a .dat file that contains lots of huge numbers such as 568e24 1.20536e8 1.3526e12 1.5145e11. And I already input these numbers as String, and stored in ArrayList. I need to use these number to calculate and represent gravitational body in Solar System. But in java it already exceeds the upper limit of Double and Int. What am I suppose to do to use these numbers in Java.

Here is my code that input these data and store these data as String in 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]);

I am trying to use BigInteger to change String to BigIntiger by following code:

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

But the output is wrong:

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)

So ...now.. what should I do to use this large number such as 1898e24. Do I need to change the format of this number (1898e24), such as 1898000000000...?

Upvotes: 0

Views: 1015

Answers (1)

Matt Clark
Matt Clark

Reputation: 28589

You could use something like BigInteger, which should be larger then you need.

Upvotes: 1

Related Questions