I am trying to create an array of 20 values the have 4 methods that 1st will create a total of all values listed, 2nd search the array and return all values less than 5.00, 3rd return the average of the values using the output method from the first method and finally display the average of all values in the array greater than the average. I am getting an error that total + prices is not a valid statement. Here is my code:

public class Prices
{
public static void main (String[] args)
{
double[] prices = {1.59,8.98,2.56,7.82,9.70,2.85,
1.98,10.80,5.05,12.76,13.20,7.36,4.86,1.25,2.05,2.70,
9.98,4.25,13.05,6.00};
double average;
double total = 0.0;
int i;
for (i = 0; i < 20; i++)
{
total + prices;

}

System.out.println("Sum of all prices: "+total+"\n" );
for (i = 0; i < 20; i++)
{
if (prices < 5.00){
System.out.println(prices + " is less than $5.00");
}
}
average = total / (20);
System.out.println("\nThe average price is " + average + "\n");
for (i = 0; i < 20; i++)
{
if (prices > average) {
System.out.println(prices + " is greater than the average");
}
}
}
}

Any help is greatly appreciated

Edited 6 Years Ago by kdgeiger: n/a

When I enter it that way I get the following:

 ----jGRASP exec: javac C:\Java\Assignments\Prices.java

Prices.java:13: operator + cannot be applied to double,double[]
 total += prices;
       ^
Prices.java:20: operator < cannot be applied to double[],double
if (prices < 5.00){
           ^
Prices.java:28: operator > cannot be applied to double[],double
if (prices > average) {
           ^
3 errors

 ----jGRASP wedge2: exit code for process is 1.
 ----jGRASP: operation complete.

I'm stumped

Edited 3 Years Ago by Nick Evan: Fixed formatting

Prices is an array, not a number. Perhaps you ought to be able to add an array to an integer and get some meaningful number, but in Java it's not so.
You need to find a way to tell the compiler what you mean. Presumably, you want the total of the values in prices? How would you go about adding those up?

(Tong - We all know you're smart now. Please let him figure it out!)

sure thought I was almost there just incorrect operators - I'll redo my code

Thanks guys - I have finally got it - now I just need to get my array total to print and I'll be done. I rewrote code with the - thanks though. I can't get my total sum to print - I am sure I have just looked at this too long. And by the way - I am a her LOL

Yes! Got it! I left out that component - ugh! Thanks Guys - for all of your help

Edited 6 Years Ago by kdgeiger: n/a

And by the way - I am a her LOL.

My apologies. You figured it out, in any case, and that's the important part. Now on to the next project, right?

Edited 6 Years Ago by jon.kiparsky: n/a

Kdgeiger, where are your four methods? I guess your teacher would like to see a program written in the following structure as you indicated at your first post:

public class Prices {
double sum(double a[]); //the 1st creates a total of all values listed
double [] search (double a[], double d);//the 2nd searches the array and returns all values less than d (e.g. 5.00)
double average(double a[]);//the 3rd returns the average of the values using the output from the first method
double[] greaterThanAverage(double a[]);//finally the 4th method displays all values in the array greater than the average
public static void main (String[] args)
{
double[] prices = {1.59,8.98,2.56,7.82,9.70,2.85,
1.98,10.80,5.05,12.76,13.20,7.36,4.86,1.25,2.05,2.70,
9.98,4.25,13.05,6.00};
.....
}
}

Edited 6 Years Ago by tong1: n/a

This question has already been answered. Start a new discussion instead.