Active IQ Unified Manager Discussions

WFA Function - Sum of an Array Elements

sheelnidhig
4,300 Views

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

4 REPLIES 4

rkiran
4,300 Views

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;

}

sheelnidhig
4,300 Views

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

rkiran
4,300 Views

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.

sheelnidhig
4,300 Views

Thanks it worked for me,

Public