如何在java selenium中将两个数组字符串的值相加并存储到另一个数组列表中?

时间:2017-11-01 09:39:46

标签: java selenium arraylist

我有4个存储至少6个值的数组。

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]

我需要获得grossAmt + TaxAmt-DiscAmt的结果并与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);

             }

但我得到了错误。

2 个答案:

答案 0 :(得分:1)

看起来金额在decimal,而您通过将其转换为integer来执行比较。我建议使用double文字类型进行比较,例如:

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);
    }
}

答案 1 :(得分:0)

尝试如下A和B是两个不同的ArrayLists

ArrayList AB = new ArrayList();

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