Need Help with a Program

I am very new to Java and I am trying to self train myself. I wanted to write a program that would add contents of an integer array and produce the sum via a return statement. Please see my code below and let me know what I am doing wrong. Please note that my code is compiling but not running from a command prompt. Thanks

public class SumIt{

static int SumIt (int iItems[]) {

int SumIt=0;

int arraylength = iItems.length;

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

SumIt=iItems+SumIt;

}

return SumIt;

}

}

[581 byte] By [chris900a] at [2007-11-27 6:14:01]
# 1

You are on the right lines but I don't think you were adding properly. You need to reference the array as below and you should use the shorthand += operator.

Take a look at this:

public class Test {

public static void main(String[] args) {

int [] ints = {1,2,2,3,4,5,};

System.out.println(getTotal(ints));

}

static int getTotal(int [] myArray) {

int sum=0;

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

sum+=myArray[i]; //Sum equals sum plus int at index

}

return sum;

}

}

Good luck with Java

Message was edited by:

_helloWorld_

_helloWorld_a at 2007-7-12 17:23:08 > top of Java-index,Java Essentials,New To Java...
# 2
That was it. Thanks so much for your help.
chris900a at 2007-7-12 17:23:08 > top of Java-index,Java Essentials,New To Java...