1. Member
Join Date
Jul 2008
Posts
3
Rep Power
0

I'm doing a project for school, I need this program to do 2 things add a list of prices and then show how many are under \$5.00. The adding part I managed to get working, wowever, I'm having a problem getting the program to show me the prices under \$5.00. I am including my code so far, any help would be greatly appreciated.

public class PricesArray
{

public static void main(String[] args)
{

double[] prices = new double[20];

prices[0] = 6.25;
prices[1] = 7.85;
prices[2] = 3.21;
prices[3] = 3.31;
prices[4] = 8.50;
prices[5] = 9.37;
prices[6] = 4.23;
prices[7] = 8.37;
prices[8] = 9.68;
prices[9] = 1.99;
prices[10] = 2.04;
prices[11] = 3.66;
prices[12] = 8.83;
prices[13] = 4.91;
prices[14] = 3.00;
prices[15] = 7.84;
prices[16] = 5.98;
prices[17] = 2.09;
prices[18] = 6.00;
prices[19] = 7.00;

System.out.println("Sum of the prices Are:");

double sum = prices[0] + prices[1] + prices[2] + prices[3] + prices[4] + prices[5] + prices[6] +
prices[7] + prices[8] + prices[9] + prices[10] + prices[11] + prices[12] + prices[13] +
prices[14]+ prices[15] + prices[16] + prices[17] + prices[18] + prices[19];

System.out.print("Sum is:" + sum);

double underFive = prices[0], prices[1], prices[2], prices[3], prices[4], prices[5], prices[6],
prices[7], prices[8], prices[9], prices[10], prices[11], prices[12], prices[13],
prices[14],prices[15], prices[16], prices[17], prices[18], prices[19];

if(underFive < 5.00)

System.out.println("Prices under 5.00 are" + underFive);

}

}

2. You want to find the values of under five. So loop the array and check the value. If the value is less than to five, that's what you want. Got it?
Last edited by Eranga; 07-04-2008 at 07:21 AM.

3. Some Java basic books you can refer oso.
I am still learning now.

4. This is array question, you can check out this basic I learnt
Arrays (The Java™ Tutorials > Learning the Java Language > Language Basics)

5. Hi,
This is just a tip, u dont have to use each value everytime like
double sum = prices[0] + prices[1] + prices[2]+ ......

and even to print the values under five u can compare the value of array with 5.00 using for loop and if loop.

You try once and v ll definitely help u if u r stuck.

6. Hey this is realy simple. But you have to learn to work with for loops or stuff like that. I will give you a little example.

To calculate the sum you have to write code like this.

Java Code:
```double sum = 0;
for(int i=0;i<prices.length;i++)
{
sum = sum + prices[i];
}```
This code will go through the whole array and adds each value in the array to the variable sum.

In other Words it does exactly this
Java Code:
```double sum = prices[0] + prices[1] + prices[2] + prices[3] + prices[4] + prices[5] + prices[6] +
prices[7] + prices[8] + prices[9] + prices[10] + prices[11] + prices[12] + prices[13] +
prices[14]+ prices[15] + prices[16] + prices[17] + prices[18] + prices[19];```
only cooler.

And if you wanna know how many are under 5

Java Code:
```int howManyUnderFive = 0;
for(int i=0;i<prices.length;i++)
if(prices[i] < 5)
howManyUnderFive++;```
Learn to use loops, you will not be able to do more stuff if you dont learn that. Its nesseary for everything, but its easy to understand. I hope it helps you

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•