954,554 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Prices Array

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

kdgeiger
Newbie Poster
11 posts since Jul 2010
Reputation Points: 10
Solved Threads: 0
 

should be
total += prices;

tong1
Posting Whiz
358 posts since Jul 2010
Reputation Points: 34
Solved Threads: 72
 

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

kdgeiger
Newbie Poster
11 posts since Jul 2010
Reputation Points: 10
Solved Threads: 0
 

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!)

jon.kiparsky
Posting Virtuoso
1,849 posts since Jun 2010
Reputation Points: 383
Solved Threads: 187
 

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

kdgeiger
Newbie Poster
11 posts since Jul 2010
Reputation Points: 10
Solved Threads: 0
 

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

jon.kiparsky
Posting Virtuoso
1,849 posts since Jun 2010
Reputation Points: 383
Solved Threads: 187
 

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

tong1
Posting Whiz
358 posts since Jul 2010
Reputation Points: 34
Solved Threads: 72
 

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 [i] - 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

kdgeiger
Newbie Poster
11 posts since Jul 2010
Reputation Points: 10
Solved Threads: 0
 

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

kdgeiger
Newbie Poster
11 posts since Jul 2010
Reputation Points: 10
Solved Threads: 0
 
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?

jon.kiparsky
Posting Virtuoso
1,849 posts since Jun 2010
Reputation Points: 383
Solved Threads: 187
 

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};
.....
}
}
tong1
Posting Whiz
358 posts since Jul 2010
Reputation Points: 34
Solved Threads: 72
 

Yep, wish me luck!

kdgeiger
Newbie Poster
11 posts since Jul 2010
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: