Flash Website Builder- Trendy Site Builder is a Flash Site Building tool that helps users build stunning websites. Check Out Custom Custom Logo Design by LogoBee. Website Design and Free Logo Templates available.
 CodingForums.com Prices java (Array)

Before you post, read our: Rules & Posting Guidelines

Enjoy an ad free experience by logging in. Not a member yet? Register.
 10-28-2008, 12:48 AM PM User | #1 The Big Boss New to the CF scene   Join Date: Oct 2008 Posts: 2 Thanks: 0 Thanked 0 Times in 0 Posts Prices java (Array) I am relative new to java and it all seems to work 50% of the time ad the other 50% isa puzzel to me. I need some simple understanding. Please !!.My problem is : Create an application containing an array that stores 20 prices, such as \$5.58,\$3.48,\$8.02,and so on. The application should (1) display the sum of all prices, (2)display all prices less than \$3.00, (3) calculate the average of the prices,and (4) display all the values that are higher than the calcuated average value. I have a start but I am sure I got los along the way, lease I neen a helping hand .Thanking you in advance. public class Prices2 { public static void main(String[] args) { int size = (20); double prices[] = new double[size]; System.out.println( "Please start entering the " + size + " prices one by one at the prompt(s)."); for (int x = 0; x < size; x++); { System.out.println("What is price " + x + "? Enter it here: "); prices[x] = read double () ; System.out.println ("There are " + (size - x - 1) + " prices still to be entered."); } double largestThusFar = prices[0]; double smallestThusFar = prices[0]; double sumSoFar = 0; for (int x = 0; x < prices.length; x++) { if (largestThusFar < prices[x]) { largestThusFar = prices[x]; } if (smallestThusFar > prices[x]) { smallestThusFar = prices[x]; } sumSoFar = sumSoFar + prices[x]; } double largest = largestThusFar; double smallest = smallestThusFar; System.out.println("The largest price is: " + largest); System.out.println("The smallest price is: " + smallest); double totalSum = sumSoFar; System.out.println("The average price is: " + totalSum / size); } }
 11-05-2008, 04:18 PM PM User | #2 Marshal New to the CF scene   Join Date: Nov 2008 Posts: 1 Thanks: 0 Thanked 0 Times in 0 Posts Hi Big Boss, I'm new to Java as well but I coded this for you. It's a bit more organized than your code so it might help you. Here it is: Code: ```import java.io.*; import java.util.*; public class Core { public static void main(String[] args) throws IOException{ double prices [] = new double[20]; double total; int option; Core c = new Core(); Scanner sc = new Scanner(System.in); do{ System.out.println("1. Enter prices"); System.out.println("2. Display sum of all prices"); System.out.println("3. Display prices lower than \$3.00"); System.out.println("4. Display average "); System.out.println("5. Display prices higher than calculated average"); System.out.println("6. Exit"); option = sc.nextInt(); switch(option){ case 1: c.addprices(prices); break; case 2: c.showtotal(); break; case 3: c.showlower(prices); break; case 4: c.showaverage(); break; case 5: c.showhigher(prices); break; } }while(option != 6); } } /****************** Core Class**********************/ import java.util.*; import java.io.*; public class Core { private double total = 0; private double average = 0; public void addprices(double [] p){ Scanner sc = new Scanner(System.in); int count = 20; for(int i = 0; i < 20; i++){ System.out.println("Add price (Remaining: "+count+"): "); p [i] = sc.nextDouble(); count--; total += p[i]; } } public void showtotal(){ System.out.println(total); } public void showlower(double [] p){ for(int i = 0; i < 20; i++){ if(p[i] < 3) System.out.println(p[i]); } } public void showaverage(){ average = total / 20; System.out.println(average); } public void showhigher(double [] p){ for(int i = 0; i < 20; i++ ){ if(p[i] > average) System.out.println(p[i]); } } }``` Last edited by Marshal; 11-05-2008 at 04:23 PM..
11-06-2008, 05:55 AM   PM User | #3
shyam
Senior Coder

Join Date: Jul 2005
Posts: 1,563
Thanks: 2
Thanked 163 Times in 160 Posts
Quote:
 Originally Posted by The Big Boss Code: ``` public class Prices2 { public static void main(String[] args) { int size = (20); double prices[] = new double[size]; System.out.println( "Please start entering the " + size + " prices one by one at the prompt(s)."); for (int x = 0; x < size; x++); { System.out.println("What is price " + x + "? Enter it here: "); prices[x] = read double () ; System.out.println ("There are " + (size - x - 1) + " prices still to be entered."); } // ... } }```
your for loop is not executing 20 times as you expect remove that offending semicolon at the end and it should work fine.
__________________
You never have to change anything you got up in the middle of the night to write. -- Saul Bellow

 Bookmarks

 Thread Tools Rate This Thread Rate This Thread: 5 : Excellent 4 : Good 3 : Average 2 : Bad 1 : Terrible

 Posting Rules You may not post new threads You may not post replies You may not post attachments You may not edit your posts BB code is On Smilies are On [IMG] code is On HTML code is Off Forum Rules
 Forum Jump User Control Panel Private Messages Subscriptions Who's Online Search Forums Forums Home :: Client side development     JavaScript programming         DOM and JSON scripting         Ajax and Design         JavaScript frameworks         Post a JavaScript     HTML & CSS     XML     Flash & ActionScript         Adobe Flex     Graphics and Multimedia discussions     General web building         Site reviews         Building for mobile devices :: Server side development     Apache configuration     Perl/ CGI     PHP         Post a PHP snippet     MySQL         Other Databases     Ruby & Ruby On Rails     ASP     ASP.NET     Java and JSP     Other server side languages/ issues         ColdFusion         Python :: Computing & Sciences     Computer Programming     Computer/PC discussions     Geek News and Humour Web Projects and Services Marketplace     Web Projects         Small projects (quick fixes and changes)         Medium projects (new script, new features, etc)         Large Projects (new web application, complex features etc)         Unknown sized projects (request quote)         Vacant job positions         Looking for work/ for hire         Project collaboration/ partnership         Paid work offers and requests (Now CLOSED)     Career, job, and business ideas or advice     Domains, Sites, and Designs for sale         Domains for sale         Websites for sale         Design templates and graphics for sale :: Other forums     Member Offers     Forum feedback and announcements

All times are GMT +1. The time now is 06:47 PM.