解析大数字时是否出现NumberFormatException? [英] NumberFormatException while parsing large numbers?

查看:133
本文介绍了解析大数字时是否出现NumberFormatException?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这可能不是最有效的编码方式,但这是我到目前为止所做的工作,直到数字变得非常大。代码应该从文件中读取2个数字,例如052和61。然后,它应该将第一个数字从最大到最小排序,以使数字尽可能大,第二个数字从最小到最大,使其尽可能小。 (520& 16)然后它打印两者的差异,在这种情况下:504.

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.

其中两个数字是3827367453723784675745843783623672348745389734268374687和1682761482512674172712635416571265716235471625176235741。从最大到最小,最小到最大的数字的排序工作正常,但是当我尝试将其解析为整数时,抛出 NumberFormatException 。我想因为数字太大而无法存储到 int 所以我尝试解析为 Long 但结果在同样的错误。

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.

我所做的是读取数字 String s然后做了 String 将每个数字存储在一个单独的索引中的数组。然后我创建了一个 int 数组,除了整数形式而不是 String 之外,我几乎拥有相同的数组。然后我对 int 数组进行了排序。然后我创建了另一个 String 来存储新的排序数字,然后我尝试解析那个 String ,那就是异常的地方被抛出。

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.

有什么建议吗?

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


推荐答案

3827367453723784675745843783623672348745389734268374687也太长了。使用 BigInteger ,它允许任意大整数。

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

这篇关于解析大数字时是否出现NumberFormatException?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆