標準入力から非常に大きな数値を読み取って、それらを合計しようとしています。
ただし、BigInteger に追加するには、次を使用する必要がありますBigInteger.valueOf(long);
。
private BigInteger sum = BigInteger.valueOf(0);
private void sum(String newNumber) {
// BigInteger is immutable, reassign the variable:
sum = sum.add(BigInteger.valueOf(Long.parseLong(newNumber)));
}
それは正常に動作しますが、BigInteger.valueOf()
のみがかかるため、の最大値 (9223372036854775807)long
より大きい数値を追加することはできません。long
9223372036854775808 以上を追加しようとするたびに、NumberFormatException が発生します (これは完全に予想されることです)。
のようなものはありBigInteger.parseBigInteger(String)
ますか?