NumberFormatException,Java 中的 BigInteger

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6330885/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 15:25:50  来源:igfitidea点击:

NumberFormatException, BigInteger in java

java

提问by CoolEulerProject

Getting following run-time error

获得以下运行时错误

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)

My Code

我的代码

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


}

}

}

please point out whats goes wrong @ runtime and why

请指出@运行时出了什么问题以及为什么

thanks in advance...

提前致谢...

回答by nicholas.hauschild

This is the line causing you grief:

这是让你悲伤的线:

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

While BigIntegerdoes work with String's, those String's must be parsable into numbers.

虽然BigInteger确实与String's一起工作,但那些String's 必须可解析为数字。

EDIT**Try this:

编辑**试试这个:

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

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

回答by Bozho

new BigInteger("z");is not meaningful. You can only pass numbers in constructor.

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

This is pretty obvious, so the next time you get an exception go the the exact line in your code shown in the exception stacktrace and you will most likely spot the problem.

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

回答by fmucar

BigInteger zz = new BigInteger("z"); 

you are passing non-numerical string thats the reason.

您正在传递非数字字符串,这就是原因。

EDIT:

编辑:

It takes string but it expects the string to be a numerical value. "z" does not have any numerical meaning.

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

回答by Buhake Sindi

BigIntegerJavadoc states for BigInteger(String value)

BigIntegerJavadoc 声明为 BigInteger(String value)

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).

将 BigInteger 的十进制字符串表示形式转换为 BigInteger。字符串表示形式由一个可选的减号后跟一个或多个十进制数字的序列组成。字符到数字的映射由 Character.digit 提供。字符串不能包含任何多余的字符(例如,空格)。

So your code:

所以你的代码:

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

is totally incorrect, but this is correct:

完全不正确,但这是正确的:

BigInteger zz = new BigInteger("5566");    

EDIT: Based on your comment, this would be simpler by using the String.valueOf()method:

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

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

回答by Pa?lo Ebermann

Could it be that you want this instead?

难道你想要这个吗?

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

    BigInteger zz = new BigInteger(z);

Note the missing quotes here. (Of course, this will only work for i < 10.)

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

回答by Kris

A common mistake is writing new BigInteger("",num) instead of new BigInteger(""+num)

一个常见的错误是写 new BigInteger("",num) 而不是 new BigInteger(""+num)