Skip to Content
avatar image
Former Member

How to find summation inside a loop?

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

Add comment
10|10000 characters needed characters exceeded

  • Get RSS Feed

2 Answers

  • Best Answer
    avatar image
    Former Member
    Aug 20, 2007 at 03:24 PM

    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

    Add comment
    10|10000 characters needed characters exceeded

    • Former Member

      Thanks Vito,

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

      Cheers,

      S

  • Aug 21, 2007 at 06:15 AM

    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

    Add comment
    10|10000 characters needed characters exceeded