Subscribe

WFA Function - Sum of an Array Elements

Hello,

I am trying to write a function which will sum all the Elements in an array...

def arrayElementSum(str)

{

          String[] splitArray = str.split(",");

          for(i=0; i<splitArray.length; i++) {

                    (int)myTotal += splitArray[i];

          }

          return myTotal;

}

This function is not working, can someone please have a look at it and see whats wrong init.

,Sheel

Re: WFA Function - Sum of an Array Elements

Try this:

def arrayElementSum(str)

{

          String[] splitArray = str.split(",");

         int i = 0;

          int myTotal = 0;

          for (i = 0; i< splitArray.length; i++) {

                    myTotal += (int) splitArray[i];

          }

          return myTotal;

}

Re: WFA Function - Sum of an Array Elements

Hello,

I tried it but when testing it get the following:

Expression: - arrayElementSum("10")

Result: - 0class java.lang.Integer

Expression:- arrayElementSum("10,10")

Result: - 0class java.lang.Integerclass java.lang.Integer

Any Idea, how can we solve this.

,Sheel

Re: WFA Function - Sum of an Array Elements

Which WFA version are you using ? The function code which I pasted earlier is working on my WFA server version 2.2.1.0.0RC1.

You can try using Integer.parseInt instead of casting to int while adding the myTotal.

Re: WFA Function - Sum of an Array Elements

Thanks it worked for me,