NumberFormatException,java中的BigInteger

时间:2011-06-13 13:14:34

标签: java

获取以下运行时错误

C:\jdk1.6.0_07\bin>java euler/BigConCheck
Exception in thread "main" java.lang.NumberFormatException: For input string: "z
"
        at java.lang.NumberFormatException.forInputString(NumberFormatException.
java:48)
        at java.lang.Integer.parseInt(Integer.java:447)
        at java.math.BigInteger.<init>(BigInteger.java:314)
        at java.math.BigInteger.<init>(BigInteger.java:447)
        at euler.BigConCheck.conCheck(BigConCheck.java:25)
        at euler.BigConCheck.main(BigConCheck.java:71)

我的代码

package euler;
import java.math.BigInteger;
class BigConCheck
{

public int[] conCheck(BigInteger big)
{
    int i=0,q=0,w=0,e=0,r=0,t=0,mul=1;
    int a[]= new int[1000];
    int b[]= new int[7];
    BigInteger rem[]= new BigInteger[4]; 
    BigInteger num[]= new BigInteger[4]; 
    for(i=0;i<4;i++)  
    num[i]=big;   // intialised num[1 to 4][0] with big
    String s="1",g="0";
    for(i=0;i<999;i++)
    s = s.concat(g);
    BigInteger divi[]= new BigInteger[4]; 

    for(i=0;i<5;i++)  
    {
        divi[i]=new BigInteger(s);  
        int z = (int)Math.pow((double)10,(double)i);

        BigInteger zz = new BigInteger("z");    // intialised div[1 to 4][0] with big
        divi[i]=divi[i].divide(zz);
    }

    for(i=0;i<996;i++)   // 5 consecative  numbers.
    {
        for(int k=0;k<5;k++)
        {
            rem[k] = num[k].mod(divi[k]);
            b[k]=rem[k].intValue();
            mul= mul*b[k]; 
            /*int z = (int)Math.pow((double)10,(double)(k+1));
        String zz = "z"; 
        BigInteger zzz = new BigInteger(zz);
        num[k]=num[k].divide(zzz);   */
        }

        a[i]=mul;
        for(int p=0;p<5;p++)
        {
            BigInteger qq = new BigInteger("10");
            num[p]=num[p].divide(qq);
        }       
    } 
    return a;
} 

public int bigestEleA(int u[])
{
    int big=0;
    for(int i=0;i<u.length;i++)
    if(big<u[i])
    big=u[i];

    return big;
}


public static void main(String args[])
{
    int con5[]= new int[1000]; 
    int punCon;
    BigInteger bigest = new BigInteger("7316717653133062491922511967442657474235534919493496983520312774506326239578318016984801869478851843858615607891129494954595017379583319528532088055111254069874715852386305071569329096329522744304355766896648950445244523161731856403098711121722383113622298934233803081353362766142828064444866452387493035890729629049156044077239071381051585930796086670172427121883998797908792274921901699720888093776657273330010533678812202354218097512545405947522435258490771167055601360483958644670632441572215539753697817977846174064955149290862569321978468622482839722413756570560574902614079729686524145351004748216637048440319989000889524345065854122758866688116427171479924442928230863465674813919123162824586178664583591245665294765456828489128831426076900422421902267105562632111110937054421750694165896040807198403850962455444362981230987879927244284909188845801561660979191338754992005240636899125607176060588611646710940507754100225698315520005593572972571636269561882670428252483600823257530420752963450");


    BigConCheck bcc = new BigConCheck();
    con5=bcc.conCheck(bigest);
    punCon=bcc.bigestEleA(con5);
    System.out.println(punCon);


}

}

请指出Wyts出现问题@ runtime以及为什么

提前感谢...

6 个答案:

答案 0 :(得分:5)

这是导致你悲伤的一条线:

BigInteger zz = new BigInteger("z");    // intialised div[1 to 4][0] with big

虽然BigInteger适用于String,但String必须可解析为数字。

修改** 试试这个:

 Integer z = (Integer)Math.pow((double)10,(double)i);

 BigInteger zz = new BigInteger(z.toString());

答案 1 :(得分:3)

new BigInteger("z");没有意义。您只能在构造函数中传递数字。

这很明显,所以下次出现异常时,请查看异常堆栈跟踪中显示的代码中的确切行,您很可能会发现问题。

答案 2 :(得分:3)

BigInteger zz = new BigInteger("z"); 

你传递非数字字符串就是原因。

编辑:

它需要字符串,但它希望字符串是一个数值。 “z”没有任何数字含义。

答案 3 :(得分:3)

BigInteger Javadoc声明BigInteger(String value)

  

翻译十进制字符串   将BigInteger表示为一个   BigInteger。字符串表示   由可选的减号组成   然后是一个或多个序列   十进制数字。字符到数字   映射由。提供   Character.digit将。字符串可能没有   包含任何无关的字符   (例如,空白)。

所以你的代码:

BigInteger zz = new BigInteger("z");    // intialised div[1 to 4][0] with big

完全错误,但这是正确的:

BigInteger zz = new BigInteger("5566");    

编辑:根据您的评论,使用String.valueOf()方法可以更简单:

int z = (int)Math.pow((double)10,(double)i);
BigInteger zz = new BigInteger(String.valueOf(z));

答案 4 :(得分:1)

可能是你想要这个吗?

    int z = (int)Math.pow((double)10,(double)i);

    BigInteger zz = new BigInteger(z);

请注意这里缺少的引号。 (当然,这只适用于i < 10。)

答案 5 :(得分:0)

一个常见的错误就是写作 新的BigInteger(&#34;&#34;,num) 代替 新的BigInteger(&#34;&#34; + num)