How do I convert a String to a BigInteger?

前端 未结 6 1412
自闭症患者
自闭症患者 2020-11-30 08:09

I\'m trying to read some really big numbers from standard input and add them together.

However, to add to BigInteger, I need to use BigInteger.valueOf(long);

6条回答
  •  鱼传尺愫
    2020-11-30 09:05

    BigInteger has a constructor where you can pass string as an argument.

    try below,

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

提交回复
热议问题