[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
• Status: Solved
• Priority: Medium
• Security: Public
• Views: 183

# adding up numbers in java

Hi all i am trying to work out a sum in Java and i am getting a error, my sum has to loop through an array and add it as it goes
here is the error i am getting and i will also show you the sum i am trying to work out and you can see if i am on the right trackwith my code

.\Calculate.java:90: operator - cannot be applied to double[],double
variance += (1/Count * (Math.pow(num - mean)));
^
1 error

``````public static double calVariance(double num[], double mean, int Count)
{
Double variance;
for (int i = 0; i <num.length; i++)
{
variance += (1/Count * (Math.pow(num - mean)));
}
System.out.println (variance);
return 1;
}
``````
Capture.PNG
0
awolarczuk
• 5
• 2
1 Solution

Commented:
Try
``````variance += (1.0/Count * (Math.pow(num[i] - mean)));
``````
0

Commented:
Scratch that - pow has two arguments - but you should get the idea - you need num[ i ] in there
0

Commented:
So
``````variance += (1.0/Count * (Math.pow(num[i] - mean, 2)));
``````
0

Commented:
So the whole method
``````public static double calVariance(double[] num, double mean, int Count) {
double variance = 0.0;

for (int i = 0; i < num.length; i++) {
variance += (1.0 / Count * (Math.pow(num[i] - mean, 2)));
}

return variance;
}
``````
0

Author Commented:
thanks so much u r grwat
0

Author Commented:
outstanding
0

Commented:

``````public static double calVariance(double[] num, double mean, int Count) {
double variance = 0.0;

for (int i = 0; i < Count; i++) {
variance += (1.0 / Count * (Math.pow(num[i] - mean, 2)));
}

return variance;
}
``````
0

## Featured Post

• 5
• 2
Tackle projects and never again get stuck behind a technical roadblock.