Options
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Mute
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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;
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Thanks it worked for me,
