Results 1 to 2 of 2
  1. #1
    dbbl21 is offline Member
    Join Date
    Mar 2013
    Rep Power

    Default Average of arrays issue

    Hi this is my first post so I hope I'm doing this correctly.
    An array of prices is to be entered by the user and then the average of the prices is supposed to be returned. I'am trying to figure out how to get this program to read the length of the array of prices that is given and not what I am initially setting "length" to. Any help would be appreciated.

    /*Complete the method named averagePrice, in the program below, named, so that it accepts an array of prices
    as a parameter, and returns the average price. You may assume all prices are double values.
    Complete the following code:*/

    import java.util.Scanner;

    public class Wiley044Prices
    {//Start of class
    A method to determine and return the average price
    public static double averagePrice(double[] priceData)
    {//Start of averagePrice

    double total = 0;

    for (double element : priceData)

    total = total + element;
    double average = 0;

    average = total / priceData.length;

    return average;

    }//End of averagePrice

    public static void main (String[] args)
    {//Start of main

    int length = 0;
    double[] priceData = new double[length];
    int currentSize = 0;
    System.out.println("Please enter prices, Q to quit: ");
    Scanner in = new Scanner(;
    while (in.hasNextDouble() && currentSize < priceData.length)
    priceData[currentSize] = in.nextDouble();

    System.out.println("The average price is " + averagePrice(priceData));
    }//End of main
    }//End of class

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Rep Power

    Default Re: Average of arrays issue

    If the array has a size of 20 and user only enters 6 values then you will need to keep track of how many by using a counter and incrementing each time a new value is entered.

    BTW you have created an array with a size of zero. Not very useful.

Similar Threads

  1. Frustrating issue with Arrays
    By knightsb78 in forum New To Java
    Replies: 2
    Last Post: 08-11-2012, 09:38 AM
  2. Calculating Average using!
    By knightwriter in forum New To Java
    Replies: 5
    Last Post: 12-06-2011, 09:02 PM
  3. Inputting arrays to find the average
    By DuncanHollows in forum New To Java
    Replies: 19
    Last Post: 07-28-2011, 06:55 PM
  4. finding average in 2d arrays
    By the beginner in forum New To Java
    Replies: 18
    Last Post: 02-24-2011, 04:32 AM
  5. Sending Arrays into methods then dividing for an average.
    By freebirdcal in forum New To Java
    Replies: 8
    Last Post: 02-03-2011, 10:41 PM

Posting Permissions

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