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

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • isaacsanya01
    New Member
    • Nov 2012
    • 6

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

    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

    Code:
     
    ArrayList<Double> fixityCheckCost = new ArrayList<Double>();
    if (action.getActionType().equals(ActionType.FIXITYCHECK) && action.getTrigger().equals(Trigger.ON_TIME))
    			    {
    			    	FixityPlugin fixityPlugin = (FixityPlugin) psiList.get(0).getPluginService().getPlugin();
    				    System.out.println(aggregationActions.getAggregation().getId() + " Aggregation Actions: " + fixityPlugin.getProcessingCostPerHour());
    				    
    				    double fixityProcessingCostPerH = fixityPlugin.getProcessingCostPerHour();
    				    
    				    double result = fixityProcessingCostPerH * 2.0;
    				    
    				    System.out.println (fixityPlugin.getAlgorithm() + " result is " + result);
    				    
    					
    					fixityCheckCost.add(result);
    					
    					double sum = 0.0;
    					
    				
    					for (int i = 0; i < fixityCheckCost.size(); i++)
    					   { 
    				    	
    				    	sum += fixityCheckCost.get(i);
    				    	i++;
    						System.out.println(fixityPlugin.getAlgorithm() + " TOTAL IS " + sum);
    					
    						 //System.out.println(sum);
    						
    					   }
    					
    					}
  • Rabbit
    Recognized Expert MVP
    • Jan 2007
    • 12517

    #2
    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.
    Last edited by Rabbit; Dec 14 '12, 04:56 PM.

    Comment

    • isaacsanya01
      New Member
      • Nov 2012
      • 6

      #3
      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
      Code:
      		ArrayList<Double> fixityCheckCost = new ArrayList<Double>();
      		    if (action.getActionType().equals(ActionType.FIXITYCHECK) && action.getTrigger().equals(Trigger.ON_TIME))
      			    {
      			    	FixityPlugin fixityPlugin = (FixityPlugin) psiList.get(0).getPluginService().getPlugin();
      			    	System.out.println(aggregationActions.getAggregation().getId() + " Aggregation Actions: " + fixityPlugin.getProcessingCostPerHour());
      
      			    	double fixityProcessingCostPerH = fixityPlugin.getProcessingCostPerHour();
      
      			    	double result = fixityProcessingCostPerH * 2.0;
      
      			    	System.out.println (fixityPlugin.getAlgorithm() + " result is " + result);
      
      
      			    	fixityCheckCost.add(result);
      
      			    	[B]int numUnique = new HashSet<Double>(fixityCheckCost).size();[/B]
      
      			    	double sum = 0.0;
      
      			    	for (int i = 0; i < numUnique; i++)
      			    	{ 
      
      			    		sum += fixityCheckCost.get(i);
      
      			    		// System.out.println(sum);
      
      			    	}
      			    	System.out.println(fixityPlugin.getAlgorithm() + " TOTAL IS " + sum);
      
      			    }

      Comment

      • Rabbit
        Recognized Expert MVP
        • Jan 2007
        • 12517

        #4
        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.

        Comment

        • isaacsanya01
          New Member
          • Nov 2012
          • 6

          #5
          yea i suppose you're correct. thanks for the input.

          Comment

          Working...