Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and the Yahoo Answers website is now in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.

Java question: Add an ArrayList of BigIntegers to one BigInteger?

This is where I read in the data from a text file, save each line as a BigInteger. I want to be able to add all of the BigIntegers in this ArrayList to one resulting BigInteger or any value what-so-ever. I have tested the program to this point and there is successful population of the ArrayList of BigIntegers.

//Read in each line as a BigInteger. Save each BigInteger to the ArrayList.

String strLine;

ArrayList<BigInteger> lines = new ArrayList<BigInteger>();

while ((strLine = br.readLine()) != null) {

lines.add(new BigInteger(strLine));

}

Thanks in advances!

2 Answers

Relevance
  • Ari
    Lv 6
    8 years ago
    Favorite Answer

    import java.util.*;

    import java.io.*;

    class BigIntegers {

    public static void main(String [ ] args) throws FileNotFoundException {

    List <BigInteger> list = new ArrayList<BigInteger>();

    Scanner scanner = new Scanner(new File("myFile.txt"));

    while(scanner.hasNext()) {

    String number = scanner.next();

    BigInteger bigInteger = new BigInteger(number);

    list.add(bigInteger);

    }

    BigInteger total = BigInteger.ZERO;

    for(BigInteger b: list) {

    total = total.add(b);

    }

    System.out.println("The sum of all of the BigIntegers was " + total);

    }

    }

  • ?
    Lv 4
    5 years ago

    it is your multiply it really is inaccurate. verify the signature of the mind-set and also you'll locate that it in common words takes one parameter. the different parameter is from the article that multiply is utilized to. So... bI= new BigInteger("2000000") gadget.out.print(bI.multiply(new BigInteger("ten thousand")));

Still have questions? Get your answers by asking now.