简体   繁体   中英

How to do sum of two array string's value and store into another array list in java selenium?

I have 4 Array that store at least 6 value.

ArrayList<String> GrossAmt = new ArrayList<String>();
ArrayList<String> TaxAmt = new ArrayList<String>();
ArrayList<String> DiscAmt = new ArrayList<String>();
ArrayList<String> FinalAmt = new ArrayList<String>();

GrossAmt = [ 66.5928,  110.9880,  369.9592,  92.4898,  249.7221,  27.7469] 
TaxAmt = [ 3.8632,  6.4386,  21.4621,  5.3655,  14.4869,  1.6097] 
DiscAmt =[ 2.2137,  3.6895,  12.2984,  3.0746,  8.3014,  0.9224] 
FinalAmt =[ 68.2423,  113.7371,  379.1229,  94.7807,  255.9076,  28.4342]

I need to get the result of grossAmt+TaxAmt-DiscAmt and compare with FinalAmt.

for(int g = 0; g < GrossAmt.size(); g++){
                int Expected = 0;
                Expected = Integer.valueOf(GrossAmt.get(g)) + Integer.valueOf(TaxAmt.get(g))- Integer.valueOf(DiscAmt.get(g));
                int Actual = Integer.valueOf(FinalAmt.get(g));
                LogFileControl.logInfo(Actual);
                LogFileControl.logInfo(Expected);


             if (Actual == Expected ){

                 LogFileControl.logInfo("Final Amount Passed!");

             }else
             {
                 LogFileControl.logError(("Actual: " + Actual) + " Expected: " + Expected);

             }

But i got error for this.

Looks like the amounts are in decimal whereas you are performing the comparison by converting them into integer . I would recommend using double literal type for this comparison, eg:

ArrayList<String> GrossAmt = new ArrayList<String>();
ArrayList<String> TaxAmt = new ArrayList<String>();
ArrayList<String> DiscAmt = new ArrayList<String>();
ArrayList<String> FinalAmt = new ArrayList<String>();

for (int g = 0; g < GrossAmt.size(); g++) {
    double Expected = 0;
    Expected = Double.valueOf(GrossAmt.get(g)) + Double.valueOf(TaxAmt.get(g))
            - Double.valueOf(DiscAmt.get(g));
    double Actual = Double.valueOf(FinalAmt.get(g));
    LogFileControl.logInfo(Actual);
    LogFileControl.logInfo(Expected);

    if (Actual == Expected) {
        LogFileControl.logInfo("Final Amount Passed!");
    } else {
        LogFileControl.logError(("Actual: " + Actual) + " Expected: " + Expected);
    }
}

Try as follows A and B are two different ArrayLists

ArrayList AB = new ArrayList();

AB.addAll(A);
AB.addAll(B);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM