Please help!!!!!

Hi. I want to print out the sum of an array. This is a simple version of my code can anyone tell me where I'm going wrong? Im getting an error about static reference of sum in main - is there a way aroung this?

publicclass temp{

publicstaticvoid main(String[] args){

double temps[] ={5.1,4.6,7.7,10.9,21.1,16.2};

System.out.println("Total: " + sum);

}

publicdouble sum(double temps[]){

int sum=0;

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

sum+=temps[i];

return(sum);

}

}

>

[1223 byte] By [javaLa] at [2007-11-27 4:37:05]
# 1
System.out.println("Total: " + sum);This is an attempt to access a variable called sum. Not a method call.Also please use more informative title in future.
floundera at 2007-7-12 9:47:17 > top of Java-index,Java Essentials,Java Programming...
# 2
Sorry Im in a bit of a panic. I am trying to get it to print the total of all the numbers. Does anyone understand what I am trying to do?
javaLa at 2007-7-12 9:47:17 > top of Java-index,Java Essentials,Java Programming...
# 3
> Sorry Im in a bit of a panic. I am trying to get it> to print the total of all the numbers. Does anyone> understand what I am trying to do?Yes.
cotton.ma at 2007-7-12 9:47:17 > top of Java-index,Java Essentials,Java Programming...
# 4
First of all change the sum variable in your sum method to be a double and not an int.
cotton.ma at 2007-7-12 9:47:17 > top of Java-index,Java Essentials,Java Programming...
# 5

Ok , can I just call the sum method from main?

The error I am getting is:

java:7: non-static variable temps cannot be referenced from a static context

public class temp {

double temps[] = {5.1,4.6,7.7,10.9,21.1,16.2,17.3,17.5,14.2,10.1,8.1,6.2};

public static void main(String[] args){

sum(temps);

}

public void sum(double temps[]) {

double sum=0.0;

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

sum+=temps[i];

//return(sum);

System.out.println("Sum: " + sum);

}

}

>

javaLa at 2007-7-12 9:47:17 > top of Java-index,Java Essentials,Java Programming...
# 6
main() is static, but sum() is not.So make sum() static.
paulcwa at 2007-7-12 9:47:17 > top of Java-index,Java Essentials,Java Programming...
# 7
> Ok , can I just call the sum method from main?> The error I am getting is:> java:7: non-static variable temps cannot be> referenced from a static context>Who told you to randomly move code about? Put the temps back in main.
cotton.ma at 2007-7-12 9:47:17 > top of Java-index,Java Essentials,Java Programming...
# 8

WOW!

This

System.out.println("Total: " + sum);

needed to be changed to this

System.out.println("Total: " + sum());

but lets make a whole bunch of changes instead.

In the words of the late great Douglas Adams "DON'T PANIC". Just take a breather and try to think clearly.

floundera at 2007-7-12 9:47:17 > top of Java-index,Java Essentials,Java Programming...