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