Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/341.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
“线程中的异常”;“主要”;java.lang.NumberFormatException:对于输入字符串:";1.0“;_Java_Long Integer_Biginteger - Fatal编程技术网

“线程中的异常”;“主要”;java.lang.NumberFormatException:对于输入字符串:";1.0“;

“线程中的异常”;“主要”;java.lang.NumberFormatException:对于输入字符串:";1.0“;,java,long-integer,biginteger,Java,Long Integer,Biginteger,我正在编写一个Java程序来对大素数进行计算,我得到以下错误: 线程“main”java.lang.NumberFormatException中的异常:对于输入字符串:“1.0” 位于java.lang.NumberFormatException.forInputString(NumberFormatException.java:65) 位于java.lang.Integer.parseInt(Integer.java:492) 在java.math.biginger.(biginger.jav

我正在编写一个Java程序来对大素数进行计算,我得到以下错误:

线程“main”java.lang.NumberFormatException中的异常:对于输入字符串:“1.0” 位于java.lang.NumberFormatException.forInputString(NumberFormatException.java:65) 位于java.lang.Integer.parseInt(Integer.java:492) 在java.math.biginger.(biginger.java:338) 在java.math.biginger.(biginger.java:476) 位于Solution.sumOfDivisorsModulo(Solution.java:24) 位于Solution.main(Solution.java:49)

公共静态BigInteger-sumOfDivisorsModulo(BigInteger n){
大整数和=(n.add(one)).mod(模);
for(biginger test=n.subtract(一);test.compareTo(新的biginger(Double.toString(Math.sqrt(n.longValue())))>=0;test.subtract(一))
{
如果(n.mod(测试)。比较到(零)==0)
{
sum=sum.add(测试);
总和=总和加(n.除(测试));
总和=总和模(模);
}
}
回报金额;
}
公共静态void main(字符串[]args){
int m=2;
int a=0;
primeList=新的大整数[m];
fillList(m);//用质数填充列表primeList,直到第m个
BigInteger n=新的BigInteger(“1”);
for(int i=0;i
one
zero
是定义为
biginger(“0”)
biginger(“1”)
的变量。 你能帮我找出问题所在吗?我先谢谢你。

问题就在这里

   new BigInteger(Double.toString(Math.sqrt(n.longValue())))
Double.toString()
调用将为您提供一个带有小数点的数字字符串。但是
biginger(String)
构造函数无法解析包含小数点的数字字符串

我不明白你想在这里做什么,但是平方根可能是一个非整数值

如果要将浮点(可能是非整数)平方根值转换为整数,则:

   // Round towards zero / truncate
   BigInteger.valueOf((long)(Math.sqrt(n.longValue())))  

这应该比通过字符串更有效。而通过
int
字符串很容易很快溢出


但是请注意,对于足够大的
n
值,平方根计算将明显不准确。除了找到或实现自己的
biginger
平方根方法之外,没有其他解决方案。但是,如果@Andreas是正确的,并且您根本不需要使用
biginger
,这是没有意义的。

对于我来说,错误消息
NumberFormatException:对于输入字符串:“1.0”
非常清楚,尤其是在尝试创建具有双值的
biginger
时。也许您想使用
BigDecimal
?为什么要定义内置的
one
zero
?参见和。另外,您知道
sum.add(test)
sum.add(n.divide(test))
不会做任何事情,对吗?您忘记将结果分配回
sum
。你用
sum=sum.mod(MODULO)
test.subtract(一)
也做对了!!!当你刚要
n.longValue()
it时,
n
test
成为
biginger
有什么意义?如果你从一个超过长的
n
值开始,你认为从
n
开始倒计时的循环会运行多久?我明白了。那么我如何从一个双精度变量中创建一个BigInteger变量呢?@Marrakchino,这取决于。你想把
的值截短还是四舍五入?我知道了,我用的是Math.floor。谢谢,呃。。。但是Math.floor会给你一个
。那么这是如何工作的呢?我将其转换为(int),然后使用String.valueOf()
   // Round towards zero / truncate
   BigInteger.valueOf((long)(Math.sqrt(n.longValue())))  
   // Round to nearest
   BigInteger.valueOf((long)(Math.round(Math.sqrt(n.longValue()))))