Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 07-04-2008, 06:46 AM
Member
 
Join Date: Jul 2008
Posts: 2
tamik0 is on a distinguished road
[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[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);








}



}
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 07-04-2008, 06:53 AM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 3,543
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
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?
__________________
Use an appropriate Subject. "Help, urgent!" isn't one.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

Has someone helped you? Then you can
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
their helpful post.

Want to make your IDE the best?
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

Last edited by Eranga : 07-04-2008 at 07:21 AM.
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 07-04-2008, 09:30 AM
janeansley's Avatar
Member
 
Join Date: Jun 2008
Posts: 23
janeansley is on a distinguished road
Some Java basic books you can refer oso.
I am still learning now.
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 07-04-2008, 09:30 AM
janeansley's Avatar
Member
 
Join Date: Jun 2008
Posts: 23
janeansley is on a distinguished road
This is array question, you can check out this basic I learnt
Arrays (The Java™ Tutorials > Learning the Java Language > Language Basics)
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 07-04-2008, 01:03 PM
Niveditha's Avatar
Senior Member
 
Join Date: May 2008
Posts: 299
Niveditha is on a distinguished road
Send a message via Skype™ to Niveditha
Hi,
This is just a tip, u dont have to use each value everytime like
double sum = prices[0] + prices[1] + prices[2]+ ......
use a for loop instead.

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.
__________________
To finish sooner, take your own time....
Nivedithaaaa
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 07-04-2008, 06:45 PM
hannehomuth's Avatar
Member
 
Join Date: Jul 2008
Location: Sommerfeld (Brandenburg, Germany)
Posts: 35
hannehomuth is on a distinguished road
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.

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

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
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT +3. The time now is 03:03 PM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org