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

Recommended Answers

All 11 Replies

should be
total += prices;

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

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

You are almost there, at least on this particular problem. The change you're looking for is a pretty minor one.

Thank you for the comments.
I was wrong. It should be:
total += prices;

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

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?

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};
.....
}
}

Yep, wish me luck!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.