Results 1 to 3 of 3
  1. #1
    tamik0 is offline Member
    Join Date
    Jul 2008
    Posts
    3
    Rep Power
    0

    Default Please Help, can't figure out what I'm doing wrong.

    Any help would be greatly appreciated, I need to display all of the amounts that are greater than the average amount(which is $6.25). The section I am working on is at the bottom of the code(in bold).


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




    System.out.print("The prices under $5.00 are");

    for(int i=0; i<prices.length; ++i)

    {

    if(prices[i]<5)

    System.out.print(prices[i]+ " ");

    }


    System.out.print("The average price is");

    double average;

    sum = 0;

    for(int i = 0; i < prices.length;i++)
    {

    sum += prices[i];

    average = sum / prices.length;

    System.out.println(prices[i]);

    }


    System.out.print("Prices higher than the average price are");

    for(int i = 0; i < prices.length; ++i)

    average = sum / prices.length;

    {

    if(average > prices[i])

    System.out.println(prices[i] + " ");

    }









    }



    }

  2. #2
    acp26b is offline Member
    Join Date
    Jul 2008
    Posts
    8
    Rep Power
    0

    Default

    for(int i = 0; i < prices.length; ++i)

    average = sum / prices.length;

    {

    if(average > prices[i])

    System.out.println(prices[i] + " ");

    }

    To fix this problem, you just need to rearrange your code, see as follows.

    Java Code:
    average = sum / prices.length;
    for(int i = 0; i < prices.length; ++i){
    
         if(average > prices[i]){
              System.out.println(prices[i] + " ");
         }
    }

  3. #3
    Niveditha's Avatar
    Niveditha is offline Senior Member
    Join Date
    May 2008
    Posts
    307
    Rep Power
    13

    Default

    Yes using loops is the most easiest way to do it.
    To finish sooner, take your own time....
    Nivedithaaaa

Similar Threads

  1. [SOLVED] Cant figure out null pointer exception
    By todd2230 in forum New To Java
    Replies: 6
    Last Post: 05-06-2008, 08:45 AM
  2. Java assignment - couple methods don't know how to figure out
    By Snowboardmylife in forum New To Java
    Replies: 1
    Last Post: 04-16-2008, 11:52 AM
  3. Replies: 3
    Last Post: 12-25-2007, 05:10 AM
  4. Seems so simple yet I can't figure it out!
    By adamhaviland in forum New To Java
    Replies: 1
    Last Post: 11-03-2007, 07:26 PM
  5. I can't figure this out
    By silvia in forum New To Java
    Replies: 3
    Last Post: 07-20-2007, 05:38 AM

Posting Permissions

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