5 Replies - 366 Views - Last Post: 07 February 2013 - 07:31 PM Rate Topic: -----

#1 jjhtwin2  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 06-November 12

Printing the numbers greater than average from Array.

Posted 07 February 2013 - 02:38 PM

Hi guys,
I am trying to print out all the numbers that are greater than the average number onto the screen. I believe it is set up correctly, as I am putting the numbers into a second array and then printing that out. But all the numbers are going in and not just the ones above the average. Been looking at other posts and not finding an answer. Any help is appreciated.


This is the output. On the main code, when I add it in a for statement to print the numbers it gets all. When i just add in the system println, it gives me the jumbled letters and numbers.

Please enter prices:
1.1
2.2
3.3
4.4
5.5
The sum of the value is $16.5
The average of these values is $3.3
The prices above average are $[D@5311a775


import java.util.*;
public class Price {
	public static void main(String args[]) {
		Scanner input = new Scanner(System.in);
		System.out.println("Please enter prices: ");
		
		double[] prices  = new double[5];
		for (int i=0; i<prices.length; i++) {
			prices[i] = input.nextDouble();
		}
		
		PriceMethods value = new PriceMethods();
		
		double aSum = value.sumArray(prices);
		System.out.println("The sum of the value is $" + aSum);
		
		double aAverage = value.aveArray(prices);
		System.out.println("The average of these values is $" + aAverage);
		
		double [] hPrices = value.highPrices(prices);
			System.out.println("The prices above average are $" + hPrices);
	}
}




public class PriceMethods {
	double sum = 0, average; //hPrices;
	double[] hPrices = new double[5];
	
	
	double sumArray(double [] prices) {
		for(int i=0; i<prices.length; i++) {
			sum+=prices[i];
		}
		return (sum);
	}
	double aveArray(double [] prices) {
		double average = sum/prices.length;
		return (average);
	}
	double[] highPrices(double [] prices) {
		for (int i =0; i<prices.length ; i++){
			if (prices[i] > average) {
				hPrices [i] = prices [i];
			}	
		}
		return hPrices;
	}
}




Is This A Good Question/Topic? 0
  • +

Replies To: Printing the numbers greater than average from Array.

#2 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2657
  • View blog
  • Posts: 11,207
  • Joined: 20-September 08

Re: Printing the numbers greater than average from Array.

Posted 07 February 2013 - 02:42 PM

Quote

System.out.println("The prices above average are $" + hPrices);


System.out.println("The prices above average are $" + java.util.Arrays.toString(hPrices));

Was This Post Helpful? 1
  • +
  • -

#3 jjhtwin2  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 06-November 12

Re: Printing the numbers greater than average from Array.

Posted 07 February 2013 - 02:45 PM

Thank you for that. I was able to print the array out, but now how do I make it do the only numbers in there are the numbers greater than the variable average.
Was This Post Helpful? 0
  • +
  • -

#4 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2197
  • View blog
  • Posts: 5,224
  • Joined: 10-September 10

Re: Printing the numbers greater than average from Array.

Posted 07 February 2013 - 02:47 PM

'hPrices' is the name of an array. The name of an array, or names of objects in general, are references to the starting memory location where the object is stored. If you simply print 'hPrices', you'll get a memory address-looking thing that doesn't help much. Now that you've seen it, you'll recognize it the next time you see it and will realize what's going on.
Was This Post Helpful? 1
  • +
  • -

#5 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2657
  • View blog
  • Posts: 11,207
  • Joined: 20-September 08

Re: Printing the numbers greater than average from Array.

Posted 07 February 2013 - 02:48 PM

Just iterate the array and print them out if > 0.0
Was This Post Helpful? 1
  • +
  • -

#6 jjhtwin2  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 06-November 12

Re: Printing the numbers greater than average from Array.

Posted 07 February 2013 - 07:31 PM

View Postg00se, on 07 February 2013 - 02:48 PM, said:

Just iterate the array and print them out if > 0.0



Took me a bit to get it working, but it works perfectly. Thank you very much, and hopefully it sticks in my head to remember for next time.

View PostGregBrannon, on 07 February 2013 - 02:47 PM, said:

'hPrices' is the name of an array. The name of an array, or names of objects in general, are references to the starting memory location where the object is stored. If you simply print 'hPrices', you'll get a memory address-looking thing that doesn't help much. Now that you've seen it, you'll recognize it the next time you see it and will realize what's going on.



Yea, it was an interesting looking result printing it like that, but I learned and thats the best way to learn something new :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1