By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,227 Members | 1,459 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,227 IT Pros & Developers. It's quick & easy.

Using loops and arrays to acquire the total sum of values in a record

P: 6
Hello there,

I need some help working with arrays and loops. i have a set of records that contains different actions. i need to iterate over the records, check the actions per record, and do some calculations based on the actions. so for example if 1 record have one action, the calculation is performed once, if another record have 2 actions, the calculation is performed twice. The issue i have is adding the total calculated sum of the actions per record. Currently, the program is totaling the result of the first record twice. i want it to total per record, please find below my current code. any advice on this is greatly appreciated. thanks

Expand|Select|Wrap|Line Numbers
  1.  
  2. ArrayList<Double> fixityCheckCost = new ArrayList<Double>();
  3. if (action.getActionType().equals(ActionType.FIXITYCHECK) && action.getTrigger().equals(Trigger.ON_TIME))
  4.                 {
  5.                     FixityPlugin fixityPlugin = (FixityPlugin) psiList.get(0).getPluginService().getPlugin();
  6.                     System.out.println(aggregationActions.getAggregation().getId() + " Aggregation Actions: " + fixityPlugin.getProcessingCostPerHour());
  7.  
  8.                     double fixityProcessingCostPerH = fixityPlugin.getProcessingCostPerHour();
  9.  
  10.                     double result = fixityProcessingCostPerH * 2.0;
  11.  
  12.                     System.out.println (fixityPlugin.getAlgorithm() + " result is " + result);
  13.  
  14.  
  15.                     fixityCheckCost.add(result);
  16.  
  17.                     double sum = 0.0;
  18.  
  19.  
  20.                     for (int i = 0; i < fixityCheckCost.size(); i++)
  21.                        { 
  22.  
  23.                         sum += fixityCheckCost.get(i);
  24.                         i++;
  25.                         System.out.println(fixityPlugin.getAlgorithm() + " TOTAL IS " + sum);
  26.  
  27.                          //System.out.println(sum);
  28.  
  29.                        }
  30.  
  31.                     }
  32.  
  33.  
Dec 14 '12 #1
Share this Question
Share on Google+
4 Replies


Rabbit
Expert Mod 10K+
P: 12,421
You are missing a lot of detail in your question so it's hard to know what's wrong but one glaring error I see is that you increment i twice. Also you print your total within the loop when it should probably be outside the loop.
Dec 14 '12 #2

P: 6
thanks for pointing out the two errors but i think i have sorted it out now with the following code below. the highlighted code did the trick. however, i would still like to understand the logic of this line of code. the question really is how do i sum up data stored in an arrayper record. so imagine i iterate over several records of information, and for each record i execute calculations, some i execute once, some i execute more than once. i wanted to find out how i display the total sum per record. not for the total sum for the overall list of records as that was what i was previously getting
Expand|Select|Wrap|Line Numbers
  1.         ArrayList<Double> fixityCheckCost = new ArrayList<Double>();
  2.             if (action.getActionType().equals(ActionType.FIXITYCHECK) && action.getTrigger().equals(Trigger.ON_TIME))
  3.                 {
  4.                     FixityPlugin fixityPlugin = (FixityPlugin) psiList.get(0).getPluginService().getPlugin();
  5.                     System.out.println(aggregationActions.getAggregation().getId() + " Aggregation Actions: " + fixityPlugin.getProcessingCostPerHour());
  6.  
  7.                     double fixityProcessingCostPerH = fixityPlugin.getProcessingCostPerHour();
  8.  
  9.                     double result = fixityProcessingCostPerH * 2.0;
  10.  
  11.                     System.out.println (fixityPlugin.getAlgorithm() + " result is " + result);
  12.  
  13.  
  14.                     fixityCheckCost.add(result);
  15.  
  16.                     int numUnique = new HashSet<Double>(fixityCheckCost).size();
  17.  
  18.                     double sum = 0.0;
  19.  
  20.                     for (int i = 0; i < numUnique; i++)
  21.                     { 
  22.  
  23.                         sum += fixityCheckCost.get(i);
  24.  
  25.                         // System.out.println(sum);
  26.  
  27.                     }
  28.                     System.out.println(fixityPlugin.getAlgorithm() + " TOTAL IS " + sum);
  29.  
  30.                 }
Dec 14 '12 #3

Rabbit
Expert Mod 10K+
P: 12,421
I don't think anyone can answer that question unless they look at the documentation for the fixity plugin that you're using. I for one have never used that plugin.
Dec 14 '12 #4

P: 6
yea i suppose you're correct. thanks for the input.
Dec 14 '12 #5

Post your reply

Sign in to post your reply or Sign up for a free account.