Suma
Suma

Reputation: 103

How to check if value is present in list in java

I want to match this Value 199.04 in TaxList. Below is my code

List<BigDecimal> TaxList = new ArrayList<BigDecimal>();   

String NetP=driver.findElement(getNetPension).getAttribute("value");
float NetPension = new Float(NetP);
log.info("Value of Net Pension on screen : "+ NetPension);

if(TaxList.contains(NetPension)) 
{   
     log.info("Income tax value from the database is matching with the value on the screen");
}
else 
{
     log.warn("Both the values are different.Net pension is:" +NetPension );
}

I am printing the TaxList in console

[199.04, 610.72, 122.12, 866.52, 94.56, 143.48, 78.28, 132.6, 12.9, 32.03, 1797.38, 128.14, 724.94]. even though value 199.04 is present.it goes to else part

Both the values are different.Net pension is:199.04

Upvotes: 0

Views: 1505

Answers (3)

Arvind Kumar Avinash
Arvind Kumar Avinash

Reputation: 79075

I believe, it is related to How to retrieve values from Result set and use it for calculations

Instead of storing the BigDecimal to the list, store the double values rounded off up to 2 places which will make your processing easier. Given below is an example program:

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.List;

class Main {
    public static void main(String[] args) {
        List<Double> taxList = new ArrayList<Double>();
        taxList.add(new BigDecimal(String.valueOf(610.72123)).setScale(2,RoundingMode.HALF_UP).doubleValue());
        taxList.add(new BigDecimal(String.valueOf(122.0072123)).setScale(2,RoundingMode.HALF_UP).doubleValue());
        taxList.add(new BigDecimal(String.valueOf(199.04123)).setScale(2,RoundingMode.HALF_UP).doubleValue());
        taxList.add(new BigDecimal(String.valueOf(78.28123)).setScale(2,RoundingMode.HALF_UP).doubleValue());
        taxList.add(new BigDecimal(String.valueOf(724.94507)).setScale(2,RoundingMode.HALF_UP).doubleValue());
        System.out.println(taxList);

        float f = 199.04f;
        double netPension = BigDecimal.valueOf(f).setScale(2,RoundingMode.HALF_UP).doubleValue();
        System.out.println(netPension);

        if (taxList.contains(netPension)) {
            System.out.println("Income tax value from the database is matching with the value on the screen");
        } else {
            System.out.println("Both the values are different. Net pension is:" + netPension);
        }
    }
}

Output:

[610.72, 122.01, 199.04, 78.28, 724.95]
199.04
Income tax value from the database is matching with the value on the screen

Upvotes: 2

kevin ternet
kevin ternet

Reputation: 4612

cast NetPension in BigDecimal :

    List<BigDecimal> TaxList = new ArrayList<BigDecimal>();   

    TaxList.add(new BigDecimal(584.32));
    TaxList.add(new BigDecimal(199.04));
    TaxList.add(new BigDecimal(201.66));

    double NetPension = 199.04;

    System.out.println("Value of Net Pension on screen : "+ NetPension);

    if(TaxList.contains(new BigDecimal(NetPension))) 
    {   
        System.out.println("Income tax value from the database is matching with the value on the screen");
    }
    else 
    {
        System.out.println("Both the values are different.Net pension is:" +NetPension );
    }

output :

Value of Net Pension on screen : 199.04

Income tax value from the database is matching with the value on the screen

Upvotes: 1

Radioo
Radioo

Reputation: 442

When calling the contains method on an ArrayList, it performs an equals() call on all contained elements. What could be happening here, is that instances of the class BigDecimal are not correctly compared to values of the float type.

Consider stepping into the contains() and check what is done with the float value when being compared to BigDecimal. Another possible solution would be to convert the float value to a BigDecimal by yourself.

Upvotes: 2

Related Questions