Click to See Complete Forum and Search --> : Java Array Question


CarlMartin10
April 11th, 2010, 07:32 PM
I have written a program that prints the contents of an array that are above a certain number (called average). It does this perfectly, but ti prints the empty elements as well (i.e. values of 0.0). How can I stop Java from printing the empty elements, or what should I do to program this correctly? New to Java here, trying to teach myself.

Thanks.

CODE:

import java.util.Scanner;
import java.util.Arrays;

// Create PriceArray Class
public class PriceArray
{

// Create main Method
public static void main(String[] args)
{
// Create array to hold 5 prices, declare variables
double[] priceList = new double[5];
Scanner sc = new Scanner(System.in);

// Get prices from the user and put them into the priceList array
System.out.println("Please enter a price: ");
int price;
for (price=0; price < priceList.length; ++price)
{
System.out.print("Price "+(price+1)+": ");
priceList[price] = sc.nextDouble();
if (priceList[price]<0)
break;
}
System.out.println("Thank you!");
System.out.println("Total: " + sumArray(priceList));
System.out.println("Average: " + averageArray(priceList));
System.out.println("High Prices: " + formatList(highPrices(priceList, averageArray(priceList))));

}
// Create sumArray Method
public static double sumArray(double[] priceList)
{
// Declare variables
double sum = 0;

//Calculate the um of the elements in the PriceList Array
for (int i = 0; i < priceList.length; i++)
{
sum += priceList[i];
}
return sum;
}
// Create averageArray Method
public static double averageArray(double[] priceList)
{
// Declare variables
double average = 0;

//Calculate the um of the elements in the PriceList Array
for (int i = 0; i < priceList.length; i++)
{
average += priceList[i] / priceList.length;
}
return average;

}
// Create highPrices Method
public static double[] highPrices(double[] priceList, double average)
{
average = averageArray(priceList);
double[] highValues = new double[5];
int j=0;
for (int i=0; i < priceList.length; ++i)
if (priceList[i] > average) highValues[j++]=priceList[i];
return highValues;
}

// Create formatList Method
public static String formatList(double[] priceList)
{
StringBuffer result = new StringBuffer();
for (int i=0; i < priceList.length; ++i)
{
if (i!=0) result.append(", ");
result.append(priceList[i]);
}
return result.toString();
}




}

dlorde
April 12th, 2010, 05:00 AM
If you must use arrays, you can either check each item for zero before printing it, or count the number of high price items before creating an array the right size to hold them (i.e. scan the price list twice). Not good.

Arrays are fixed-length containers, which are fine when you know in advance how many items you'll be dealing with, and that number never changes (e.g. days of week, months of the year, letters in the alphabet, etc). They're not so good for variable numbers of items. Your high prices array could cause nasty problems if you decide to process more than 5 price list items.

To make handling variable numbers of items simpler and safer, there are collection classes such as ArrayList (http://java.sun.com/j2se/1.5.0/docs/api/java/util/ArrayList.html). I recommend you use them.

If you don't think carefully, you might believe that programming is just typing statements in a programming language...
W. Cunningham