0

I have list of web element there are totally 3 values in list of web element I want to do addition of that values in BigDecimal I have return following code

Public BigDecimal calculatebigdecimal{
List<WebElement> elementValues = driver.findElements(By.xpath(“xpath to element”));
String values;
BigDecimal elementValueTotal= null;
BigDecimal sum = null;
for(int i =0; i<elementValues.size();i++)
{
values = elementValues.get(i).getText();
elementTotal = new BigDecimal(values);
elementTota.plus();
}
return elementTotal;
niqueco
  • 2,261
  • 19
  • 39

2 Answers2

1

Replace the ArrayList with appropriate elements from Web Site.

  public BigDecimal calculatebigdecimal() {
    List<String> elementValues = new ArrayList<String>();
    elementValues.add("1");
    elementValues.add("2");
    elementValues.add("3");
    String values;
    BigDecimal elementValueTotal = new BigDecimal(0);

    for (int I = 0; I < elementValues.size(); I++) {
        values = elementValues.get(I);
        elementValueTotal = elementValueTotal.add(new BigDecimal(values));

    }

    System.out.println(elementValueTotal);
    return elementValueTotal;
}
Ashraff Ali Wahab
  • 1,088
  • 9
  • 19
0

Bigdecimal is immutable, so you gotta reassign the element. See:

elementTotal = elementTotal.add(BigDecimal.ONE);

Remember to assign elementValueTotal to 0 before the for iteration.

Guilherme Alencar
  • 1,243
  • 12
  • 21