NumberFormatException异常,BigInteger的在Java
获得以下运行时错误NumberFormatException异常,BigInteger的在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);
}
}
请指出什么不顺心@运行时,为什么
感谢提前...
这是造成你解忧线:
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());
嘿谢谢,但同样的错误 – CoolEulerProject 2011-06-13 13:22:52
后字符串gg =“z”; \t \t \t BigInteger zz = new BigInteger(“gg”); – CoolEulerProject 2011-06-13 13:23:13
@ user727154,你**不能**传递非数字字符串! '“gg”'不是一个数字。 – 2011-06-13 13:28:01
new BigInteger("z");
是没有意义的。你只能在构造函数中传递数字。
这很明显,所以下一次出现异常时,请执行代码中显示的确切代码行,并且很可能会发现问题。
String gg =“z”; BigInteger zz = new BigInteger(gg); \t 同样的错误 – CoolEulerProject 2011-06-13 13:35:21
@ user727154您希望用'z'字符串表示什么数字? BigInteger只保存数字。 – Bozho 2011-06-13 13:40:37
不是一个字符串,但是由于BigInteger只将字符串作为参数,所以我不得不这样做,但它不起作用。所以我不得不将int整型到Integer类中,并调用toSting()以字符串形式传递参数,谢谢......现在开始工作 – CoolEulerProject 2011-06-13 14:24:09
BigInteger zz = new BigInteger("z");
您正在传递非数字字符串多数民众赞成的原因。
编辑:
这需要字符串,但它期望的字符串是一个数值。 “z”没有任何数字意义。
String gg =“z”; \t \t \t BigInteger zz = new BigInteger(gg); – CoolEulerProject 2011-06-13 13:29:59
同样的错误,BigInteger以字符串作为参数,仍然错误是相同的 – CoolEulerProject 2011-06-13 13:30:39
它需要字符串,但它期望字符串是一个数值。 “z”没有数字意义。 – fmucar 2011-06-13 14:26:07
BigInteger
为BigInteger(String value)
的Javadoc状态将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));
String gg =“z”; BigInteger zz = new BigInteger(gg); – CoolEulerProject 2011-06-13 13:34:12
仍然是相同的错误 – CoolEulerProject 2011-06-13 13:34:39
@ user727154,字符串''z“'不是数字。你了解单词**数字**的含义吗? – 2011-06-13 13:35:09
难道它是你想要这个吗?
int z = (int)Math.pow((double)10,(double)i);
BigInteger zz = new BigInteger(z);
请注意这里缺少的引号。 (当然,这只会为i < 10
工作。)
谢谢,是的,只有当z已经在0-9的限制内,它才会起作用,但除此之外,因为它的工作原理,我不得不将int整型到Integer类并调用toSting()将参数作为字符串传递,谢谢......现在正在工作 – CoolEulerProject 2011-06-13 14:20:58
一个常见的错误是写 新的BigInteger( “”,NUM)的 代替 新的BigInteger( “” + NUM)
需要有人来解释为什么他们首先提出了这个问题。或者检查他们的药物。 – 2011-06-13 13:37:48