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

## [SOLVED] Please Help

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;

prices = 6.25;
prices = 7.85;
prices = 3.21;
prices = 3.31;
prices = 8.50;
prices = 9.37;
prices = 4.23;
prices = 8.37;
prices = 9.68;
prices = 1.99;
prices = 2.04;
prices = 3.66;
prices = 8.83;
prices = 4.91;
prices = 3.00;
prices = 7.84;
prices = 5.98;
prices = 2.09;
prices = 6.00;
prices = 7.00;

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

double sum = prices + prices + prices + prices + prices + prices + prices +
prices + prices + prices + prices + prices + prices + prices +
prices+ prices + prices + prices + prices + prices;

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

double underFive = prices, prices, prices, prices, prices, prices, prices,
prices, prices, prices, prices, prices, prices, prices,
prices,prices, prices, prices, prices, prices;

if(underFive < 5.00)

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

}

}  Reply With Quote

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 06:21 AM.  Reply With Quote

3. ## Some Java basic books you can refer oso.
I am still learning now.  Reply With Quote

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

5. ## Hi,
This is just a tip, u dont have to use each value everytime like
double sum = prices + prices + prices+ ......

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.  Reply With Quote

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 + prices + prices + prices + prices + prices + prices +
prices + prices + prices + prices + prices + prices + prices +
prices+ prices + prices + prices + prices + prices;```
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  Reply With Quote

#### Posting Permissions

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