Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Printing Array without printing empty elements

  1. #1
    Junior Member
    Join Date
    Apr 2010
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Printing Array without printing empty elements

    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?

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


  2. #2
    Member
    Join Date
    Apr 2010
    Location
    The Hague, Netherlands
    Posts
    91
    Thanks
    3
    Thanked 10 Times in 10 Posts

    Default Re: Printing Array without printing empty elements

    Before printing, check if the value is not empty and 0.0?

    ---
    edit: I just noticed there is a [SOLVED] tag in front of this topic, does the code work now CarlMartin10?
    Last edited by Bryan; April 12th, 2010 at 06:08 AM.

Similar Threads

  1. JOptionPane Question/ Printing
    By 03EVOAWD in forum AWT / Java Swing
    Replies: 2
    Last Post: August 31st, 2009, 09:17 AM
  2. Printing a JTable
    By hundu in forum AWT / Java Swing
    Replies: 0
    Last Post: June 29th, 2009, 08:15 AM
  3. How to printing a Jtable
    By hundu in forum AWT / Java Swing
    Replies: 0
    Last Post: June 29th, 2009, 06:57 AM
  4. Printing JTable that retrieve data from the Database
    By hundu in forum AWT / Java Swing
    Replies: 3
    Last Post: June 28th, 2009, 01:50 PM
  5. Printing a Histogram Help - Arrays
    By Mock26 in forum Collections and Generics
    Replies: 1
    Last Post: June 4th, 2009, 04:49 AM