2013-04-13 106 views
0

下面的代码是用于调试:Java的BigInteger的操作

public static void main(String[] args) { 
    BigInteger n = new BigInteger("10000000001"); 
    String sn = n.toString(); 
    char[] array = sn.toCharArray(); 
    //intend to change value of some char in array 
    //not standard math operation 
    BigInteger result = new BigInteger(array.toString()); 
} 

它给我的错误:

Exception in thread "main" java.lang.NumberFormatException: For input string: "[[email protected]" 
     at java.lang.NumberFormatException.forInputString(NumberFormatException.java:48) 
     at java.lang.Integer.parseInt(Integer.java:449) 
     at java.math.BigInteger.<init>(BigInteger.java:316) 
     at java.math.BigInteger.<init>(BigInteger.java:451) 
     at debug.main(debug.java:14) 

但它工作正常,直到这时,我不太清楚什么地方错误。

+0

尝试打印'与Array.toString()'因为它似乎是对的BigInteger构造一个无效的输入字符串。 – effeffe

+0

'它工作正常,直到这种情况'你能显示一些其他情况下工作正常吗? – Pshemo

+0

我发誓它工作了一段时间..感谢您的所有努力,它也困惑我,它以某种方式它为少数输入工作... – Miranda

回答

4

如有疑问,请添加更多诊断信息......取出做两件事的陈述。这个:

array.toString() 

不会做你期望的事情,因为数组并没有覆盖toString()。它会返回类似"[[email protected]"。您可以通过提取额外的局部变量看到这一点:

BigInteger n = new BigInteger("10000000001"); 
String sn = n.toString(); 
char[] array = sn.toCharArray(); 
String text = array.toString(); 
BigInteger result = new BigInteger(text); 

然后在调试器中,你应该能够轻松地看看text调用BigInteger前值 - 这将显示问题很清楚。

为char数组转换为包含这些字符的字符串,你想:

new String(array)