user11478498
user11478498

Reputation:

how to fix error: not a statement with += on an array

I am attempting to create a Mean Absolute Deviation calculator, with a user-defined number of entries, which I accomplished with an array, adding elements in a for loop. The actual calculation is set up correctly, but i keep getting an error while compiling.

I have tried stating the variable and then using the += operator, only to receive 4 errors for each of the for loops. It simply says- error: not a statement. the error is specifically in distances[bruh] right on the opening of [].

for(int bruh2 = 0; bruh2 < ude; bruh2++){
    double halfofmean1 += points[bruh2];

I would like to see halfofmean1 be set to the sum of all elements in the points array(which is a double array), but it keeps showing errors while compiling.

Upvotes: 0

Views: 431

Answers (2)

Mark Wheeler
Mark Wheeler

Reputation: 574

Guessing here, but shouldn't halfOfMean be declared before the loop (and initialized to zero)?

Upvotes: 0

Elliott Frisch
Elliott Frisch

Reputation: 201477

I'm fairly certain you meant to sum the elements in points with your loop, to do so you must declare and initialize halfofmean1 before the loop. Like,

double halfofmean1 = 0;
for(int bruh2 = 0; bruh2 < ude; bruh2++){
    halfofmean1 += points[bruh2];
}

If you're using Java 8+, you could use a DoubleStream to sum the points like

double halfofmean1 = Arrays.stream(points).sum();

Upvotes: 1

Related Questions