问题描述
|
获取以下运行时错误
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());
, new BigInteger(\"z\");
没有意义。您只能在构造函数中传递数字。
这是很明显的,因此,下次获得异常时,请在异常堆栈跟踪中显示的代码中准确显示一行,您很可能会发现问题。
, BigInteger
的Javadoc状态states9
翻译十进制字符串
将BigInteger表示为
BigInteger。字符串表示
由可选的减号组成
随后是一个或多个序列
十进制数字。字符到数字
映射由提供
字符数字。字符串可能不会
包含任何无关的字符
(例如,空白)。
所以你的代码:
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));
, BigInteger zz = new BigInteger(\"z\");
您正在传递非数字字符串,这就是原因。
编辑:
它使用字符串,但期望字符串为数字值。 \“ z \”没有任何数字含义。
, 可能是您想要这个吗?
int z = (int)Math.pow((double)10,(double)i);
BigInteger zz = new BigInteger(z);
请注意此处缺少的引号。 (当然,这仅适用于i < 10
。)
, 一个常见的错误是写作
新的BigInteger(\“ \”,num)
代替
新的BigInteger(\“ \” + num)