NumberFormatException,java中的BigInteger
获取以下运行时错误NumberFormatException,java中的BigInteger,java,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.I
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)
C:\jdk1.6.0\u 07\bin>java euler/BigConCheck
线程“main”java.lang.NumberFormatException中的异常:对于输入字符串:“z”
"
在java.lang.NumberFormatException.forInputString(NumberFormatException。
爪哇:48)
位于java.lang.Integer.parseInt(Integer.java:447)
在java.math.biginger.(biginger.java:314)
在java.math.biginger.(biginger.java:447)
位于euler.BigConCheck.conCheck(BigConCheck.java:25)
位于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);
}
package;
导入java.math.biginger;
类BigConCheck
{
public int[]conCheck(biginger-big)
{
int i=0,q=0,w=0,e=0,r=0,t=0,mul=1;
整数a[]=新整数[1000];
int b[]=新int[7];
BigInteger rem[]=新的BigInteger[4];
BigInteger num[]=新的BigInteger[4];
对于(i=0;i这是引起你悲伤的线:
BigInteger zz = new BigInteger("z"); // intialised div[1 to 4][0] with big
虽然biginger
与String
一起工作,但那些String
必须可分解为数字
编辑**
试试这个:
Integer z = (Integer)Math.pow((double)10,(double)i);
BigInteger zz = new BigInteger(z.toString());
new BigInteger(“z”);
没有意义。只能在构造函数中传递数字
这是非常明显的,所以下次当你得到一个异常的时候,去你的代码中异常堆栈跟踪中显示的那一行,你很可能会发现问题
BigInteger zz = new BigInteger("z");
您正在传递非数字字符串,这就是原因
编辑:
它接受字符串,但希望字符串是数值。“z”没有任何数值含义。biginger
Javadoc为biginger(字符串值)
转换十进制字符串
将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));
是不是你想要这个
int z = (int)Math.pow((double)10,(double)i);
BigInteger zz = new BigInteger(z);
请注意此处缺少的引号。(当然,这只适用于i<10
)常见的错误是书写
新的BigInteger(“,num)
而不是
新的BigInteger(“+num)@user727154,您不能传递非数字字符串!“gg”
不是数字。@user727154,您使用的BigInteger构造函数接受可以转换为以10为基数的字符串。z
和gg
不能转换,除非您使用的是err Base 64?@user727154,“z”是一个字符串文字,表示“z”的实际字符串值。您的印象是“z”会将int原语转换为表示底层数字的字符串,而Java中不会这样做。也许您希望使用integer对象并调用.toString()在它上面,并将其传递到BigInteger构造函数中。@user727154,Java中的字符串literal“z”
是一个对象。如果您来自C,则它不是一个字符(是一个int)。这就是为什么它将继续在任何字符串上失败,除非该字符串包含[0-9]仅限@user727154,@nicholas.hauschild,您也可以使用String.valueOf()
方法。String gg=“z”;biginger zz=new biginger(gg);同样的错误,biginger将String作为参数,仍然是sameIt,但它希望字符串是数值。“z”没有数字含义。String gg=“z”;biginger zz=new biginger(gg);@user727154,字符串“z”
不是数字。您理解单词numeric的含义吗?我想,从各个方面看,z是数字的,因为int z=(int)Math.pow((double)10,(double)I);在代码中,问题是它必须是字符串,所以我将它放入Integer类并调用toString()若要将其转换为字符串,它就起作用了,谢谢。@user727154,感谢您更正您想要使用z
变量并创建一个BigInteger。我已经更新了我的帖子。String gg=“z”;BigInteger zz=新的BigInteger(gg);一样error@user727154您希望用“z”字符串表示什么数字?BigInteger只包含数字。不是字符串,但由于BigInteger只将字符串作为参数,所以我必须这样做,但它不起作用。因此我必须将int扭曲为Integer类并调用toSting()要以字符串形式传递参数,谢谢…现在可以了。有人需要解释为什么他们一开始就对其进行了升级。或者检查他们的MED。谢谢,是的,只有当z处于0-9限制时,它才会起作用,但在其他情况下,由于它起作用,我必须将int扭曲为整数类并调用toSting()要以字符串形式传递参数,谢谢…现在可以工作了