Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.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 如何将字符串转换为BigInteger?_Java_String_Biginteger - Fatal编程技术网

Java 如何将字符串转换为BigInteger?

Java 如何将字符串转换为BigInteger?,java,string,biginteger,Java,String,Biginteger,我试图从标准输入中读取一些非常大的数字,并将它们相加 但是,要添加到biginger,我需要使用biginger.valueOf(long): 这很好,但是由于biginger.valueOf()只需要long,我无法添加大于long最大值(9223372036854775807)的数字 无论何时我尝试添加9223372036854775808或更多,我都会得到一个NumberFormatException(这完全是意料之中的) 是否有类似于使用构造函数的biginger.parsebiging

我试图从标准输入中读取一些非常大的数字,并将它们相加

但是,要添加到biginger,我需要使用
biginger.valueOf(long)

这很好,但是由于
biginger.valueOf()
只需要
long
,我无法添加大于
long
最大值(9223372036854775807)的数字

无论何时我尝试添加9223372036854775808或更多,我都会得到一个NumberFormatException(这完全是意料之中的)

是否有类似于使用构造函数的
biginger.parsebiginger(String)

BigInteger(字符串值)

将BigInteger的十进制字符串表示形式转换为BigInteger

根据:

BigInteger(字符串值)

将BigInteger的十进制字符串表示形式转换为BigInteger

这意味着您可以使用
String
初始化
biginger
对象,如以下代码段所示:

sum = sum.add(new BigInteger(newNumber));

BigInteger有一个构造函数,您可以在其中将字符串作为参数传递

试试下面

private void sum(String newNumber) {
    // BigInteger is immutable, reassign the variable:
    this.sum = this.sum.add(new BigInteger(newNumber));
}

您可以直接使用接受字符串参数的BigInteger构造函数,而不是使用
valueOf(long)
parse()

BigInteger numBig = new BigInteger("8599825996872482982482982252524684268426846846846846849848418418414141841841984219848941984218942894298421984286289228927948728929829");

这将为您提供所需的值。

对于要将
字符串的
数组
转换为
大整数的
数组的循环,请执行以下操作:

String[] unsorted = new String[n]; //array of Strings
BigInteger[] series = new BigInteger[n]; //array of BigIntegers

for(int i=0; i<n; i++){
    series[i] = new BigInteger(unsorted[i]); //convert String to bigInteger
}
String[]未排序=新字符串[n]//字符串数组
BigInteger[]系列=新的BigInteger[n]//大整数数组

对于(int i=0;i如果您可能希望将纯文本(而不仅仅是数字)转换为BigInteger,那么如果您只是尝试:new BigInteger(“非数字”),您将遇到一个异常

在这种情况下,您可以这样做:

public  BigInteger stringToBigInteger(String string){
    byte[] asciiCharacters = string.getBytes(StandardCharsets.US_ASCII);
    StringBuilder asciiString = new StringBuilder();
    for(byte asciiCharacter:asciiCharacters){
        asciiString.append(Byte.toString(asciiCharacter));
    }
    BigInteger bigInteger = new BigInteger(asciiString.toString());
    return bigInteger;
}

我也尝试了同样的方法,但遇到了问题,因为我没有导入java.math.biginger
public  BigInteger stringToBigInteger(String string){
    byte[] asciiCharacters = string.getBytes(StandardCharsets.US_ASCII);
    StringBuilder asciiString = new StringBuilder();
    for(byte asciiCharacter:asciiCharacters){
        asciiString.append(Byte.toString(asciiCharacter));
    }
    BigInteger bigInteger = new BigInteger(asciiString.toString());
    return bigInteger;
}