Oscar F
Oscar F

Reputation: 323

NumberFormatException while parsing large numbers?

I know this probably isn't the most efficient way of coding this but it's what I have so far and it works up until the numbers get really big. The code should read 2 numbers from a file for example "052" and "61". It should then sort the first number from greatest to least to make the number as large as possible and the second number from least to greatest to make it as small as possible. (520 & 16) Then it prints the difference of the two, in this case: 504.

Two of the numbers are "3827367453723784675745843783623672348745389734268374687" and "1682761482512674172712635416571265716235471625176235741". The sorting of the numbers from greatest to least and least to greatest works fine, but when I try to parse it into an integer a NumberFormatException is thrown. I figured because the number was too large to be stored into an int so I tried parsing into a Long but that resulted in the same error.

What I did was read the numbers as Strings then made a String array storing each number in a separate index. Then I made an int array where I pretty much had the same array except in integer form instead of a String. I then sorted the int arrays. Then I created another String to store the new sorted numbers, then I tried to parse that String and that's where the exception is thrown.

Any suggestions?

import java.io.*;    
import java.util.*;    
import java.text.*;    
import static java.lang.System.*;    

public class BigDif    
{    
    public static void main(String[] args) throws IOException    
    {    
        Scanner scan = new Scanner (new File ("BigDif.dat"));    
        int numRuns = scan.nextInt();    
        scan.nextLine();    

    for (int i = 0; i < numRuns; i++)    
    {
        String firstNum = scan.nextLine();
        String secondNum = scan.nextLine();
        String[] firstNum2 = new String[firstNum.length()];
        String[] secondNum2 = new String[secondNum.length()];
        int[] firstNum3 = new int[firstNum.length()];
        int[] secondNum3 = new int[secondNum.length()];
        int big = 0;
        int notBig = 0;
        String firstNum4 = null;
        String secondNum4 = null;
        int firstNum5 = 0;
        int secondNum5 = 0;
        for (int j = 0; j < firstNum.length(); j++)
        {
            firstNum2[j] = Character.toString(firstNum.charAt(j));
        }
        for (int j = 0; j < secondNum.length(); j++)
        {
            secondNum2[j] = Character.toString(secondNum.charAt(j));
        }
        for (int j = 0; j < firstNum2.length; j++)
        {
            firstNum3[j] = Integer.parseInt(firstNum2[j]);
            secondNum3[j] = Integer.parseInt(secondNum2[j]);
        }
        Arrays.sort(firstNum3);
        Arrays.sort(secondNum3);
        for (int m = 0; m < firstNum3.length; m++)
        {
            if (m == 0)
                firstNum4 = (Integer.toString(firstNum3[m]));
            else
                firstNum4 = (Integer.toString(firstNum3[m]))+ firstNum4;
        }

        for (int m = 0; m < secondNum3.length; m++)
        {
            if (m == 0)
                secondNum4 = (Integer.toString(secondNum3[m]));
            else
                secondNum4 += (Integer.toString(secondNum3[m]));
        }

        firstNum5 = Integer.parseInt(firstNum4); //the exception is thrown here
        secondNum5 = Integer.parseInt(secondNum4);

        if (firstNum5 >= secondNum5)
        {
            big = firstNum5;
            notBig = secondNum5;
        }
        else
        {
            big = secondNum5;
            notBig = firstNum5;
        }

        out.println(big - notBig);
    }
    }    
}    

Upvotes: 7

Views: 7252

Answers (2)

Rahul Tripathi
Rahul Tripathi

Reputation: 172418

You may try to use BigInteger as the range of BigIntegers will allow you to store this big number as 1682761482512674172712635416571265716235471625176235741 and 3827367453723784675745843783623672348745389734268374687 are too large numbers and they are beyond the range of integers.

Try the BigInteger(String val) constructor

Translates the decimal String representation of a BigInteger into a BigInteger. The String representation consists of an optional minus sign followed by a sequence of one or more decimal digits. The character-to-digit mapping is provided by Character.digit. The String may not contain any extraneous characters (whitespace, for example).

Upvotes: 6

JB Nizet
JB Nizet

Reputation: 691715

3827367453723784675745843783623672348745389734268374687 is too large for a long as well. Use BigInteger, which allows arbitrary large integer numbers.

Upvotes: 11

Related Questions