samedi 23 juin 2018

FOR loop - while debugging each iteration works fine but when I run it I have different output [JAVA]

I'm working as an intern and I'm trying to build a Java Swing desktop application for my employer. The basic idea of the application is that it imports large data sets and calculate some of the cells in the row and adds new elements to each row based on those calculations. Whole data is stored in a Vector of vectors (tableData) and it is displayed afterwards in a JTable.

        // Calculating YTD
    DataImport.tableHeaders.add(8, "Budget YTD");
    DataImport.tableHeaders.add(9, "Actual YTD");
    DataImport.tableHeaders.add(10, "Variance YTD");
    double budgetYTD = 0;
    double actualYTD = 0;
    Object costCentre = tableData.get(0).get(0);
    Object expenseHead = tableData.get(0).get(1);
    TreeMap<String, ArrayList<Double>> visitedElements = new TreeMap<String, ArrayList<Double>>();
    for (int s = 0; s< y; s++) {

        String head = costCentre.toString() + expenseHead.toString();

        double currentBudget = Double.parseDouble(tableData.get(s).get(5));
        double currentActual = Double.parseDouble(tableData.get(s).get(6));

        if (costCentre.equals(tableData.get(s).get(0)) && expenseHead.equals(tableData.get(s).get(1))) {

            budgetYTD += currentBudget;
            tableData.get(s).add(8, roundOffTo2DecPlaces(budgetYTD));

            actualYTD += currentActual;
            tableData.get(s).add(9, roundOffTo2DecPlaces(actualYTD));

            double varianceYTD = budgetYTD - actualYTD;
            tableData.get(s).add(10, roundOffTo2DecPlaces(varianceYTD));
        }

        else if (visitedElements.containsKey(head)) {
            budgetYTD = visitedElements.get(head).get(0) + currentBudget;
            actualYTD = visitedElements.get(head).get(1) + currentActual;
            visitedElements.get(head).remove(0);
            visitedElements.get(head).remove(0);
            visitedElements.get(head).add(budgetYTD);
            visitedElements.get(head).add(actualYTD);
            double varianceYTD = budgetYTD - actualYTD;
            tableData.get(s).add(8, roundOffTo2DecPlaces(budgetYTD));
            tableData.get(s).add(9, roundOffTo2DecPlaces(actualYTD));
            tableData.get(s).add(10, roundOffTo2DecPlaces(varianceYTD));
            costCentre = tableData.get(s).get(0);
            expenseHead = tableData.get(s).get(1);
        }

        else {
            ArrayList<Double> list = new ArrayList<Double>();
            list.add(budgetYTD);
            list.add(actualYTD);
            String newHead = costCentre.toString() + expenseHead.toString();
            visitedElements.put(newHead, list);
            costCentre = tableData.get(s).get(0);
            expenseHead = tableData.get(s).get(1);
            budgetYTD = 0;
            actualYTD = 0;

            currentBudget = Double.parseDouble(tableData.get(s).get(5));
            budgetYTD += currentBudget;
            tableData.get(s).add(8, roundOffTo2DecPlaces(budgetYTD));

            currentActual = Double.parseDouble(tableData.get(s).get(6));
            actualYTD += currentActual;
            tableData.get(s).add(9, roundOffTo2DecPlaces(actualYTD));

            double varianceYTD = budgetYTD - actualYTD;
            tableData.get(s).add(10, roundOffTo2DecPlaces(varianceYTD));
        }
    }

This piece of code should calculate 3 fields according to the other existing values in tableData and keep always track if a row has been visited (with the head variable), if it has already been visited the calculation is made by adding to the old value of that element the current one specific to that row, so my map holds the key - which is the element and a list of 2 double variables which represents the most recent values of that element after it has been calculated.

Eg:

map -> (head1, <1, 2>), (head2, <1, 2>), (head3, <1, 2>)

x iteration in loop -> found head2 with (5, 6) in that row calculate 5+1 and 6+2 add them to table update values in map

The only problem is that if i run the code, the else if statement seems to not fire but when I'm debugging, it fires and everything behaves as expected. I'm really confused and I run out of ideas. Can anyone share their opinion about this?

Aucun commentaire:

Enregistrer un commentaire