Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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);
    }
    }    
}    
like image 254
Oscar F Avatar asked Mar 04 '14 06:03

Oscar F


1 Answers

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

like image 101
JB Nizet Avatar answered Nov 01 '22 02:11

JB Nizet