cancel
Showing results for 
Search instead for 
Did you mean: 

How to find summation inside a loop?

Former Member
0 Kudos

Hi Experts,

Please refer following code:

for (int i=0 ; i<wdContext.nodeWorklist().size() ; i++){

wdContext.nodeWorklist().setLeadSelection(i);

categoryElement = wdContext.createCategoriesElement();

category_1Element = wdContext.createCategories_1Element();

wbs = wdContext.currentWorklistElement().getReciever_Wbs_Element();

sum_hrs = wdContext.currentWorklistElement().getSum_Hours();

sum_hrs = sum_hrs.replaceAll(",",".");

if( sum_hrs != null && sum_hrs.trim().length()>0)

d = Double.parseDouble(sum_hrs) ;

d1 = d + 0;

categoryElement.setCategoryText(wbs);

categoryElement.setSeries1Value(d);

category_1Element.setSeries1Value(d1);

wdContext.nodeCategories().addElement(categoryElement);

wdContext.nodeCategories_1().addElement(category_1Element);

wbs = "";

}

I have to find summation of sum_hrs. I don't know how to go about it. I have tried using following line. However it is not working.

d1 = d + 0;

Suppose wdContext.nodeWorklist().size() = 4. In this case the loop will executed four times. Suppose in four cases sum_hrs are 2,3,5 and 2. I have to find the summation of 235+2 = 12.

Can you please help me in modifying the code in such a way that 235+2 = 12 operation is getting executed.

Thanks,

S

Accepted Solutions (1)

Accepted Solutions (1)

Former Member
0 Kudos

Hi Stuart,

please, try this:

sumValue = 0;

for (......)

{

.....

actualValue = Double.parseDouble(sum_hrs) ;

sumValue = sumValue + actualValue;

.....

}

sumValue will contain the sum of all numbers.

Hope this help you.

Vito

Former Member
0 Kudos

Thanks Vito,

The issue has got resolved using your concept. Full marks to you for the help.

Cheers,

S

Answers (1)

Answers (1)

former_member185029
Active Contributor
0 Kudos

Hello,

Please check following code.



for (int i=0 ; i<wdContext.nodeWorklist().size() ; i++){
wdContext.nodeWorklist().setLeadSelection(i);
categoryElement = wdContext.createCategoriesElement();
double finalSum =0.0;
category_1Element = wdContext.createCategories_1Element();
wbs = wdContext.currentWorklistElement().getReciever_Wbs_Element(); 
sum_hrs = wdContext.currentWorklistElement().getSum_Hours();
sum_hrs = sum_hrs.replaceAll(",",".");
if( sum_hrs != null && sum_hrs.trim().length()>0)
d = Double.parseDouble(sum_hrs) ;
finalSum=finalSum+d;
categoryElement.setCategoryText(wbs);
categoryElement.setSeries1Value(d);
category_1Element.setSeries1Value(d1);
wdContext.nodeCategories().addElement(categoryElement);
wdContext.nodeCategories_1().addElement(category_1Element);
wbs = "";
}

Ashutosh