I have to store the data in a 2D array and then print the average of the price which has available option "no", can anybody help to find the average.
public class Test {
public static void main(String[] args) {
double total = 0;
Products[][] names = new Products[][]{
{new Products("k7580", 6500, "yes")},
{new Products("k8570", 7333, "no")},
{new Products("k8580", 766, "no")},
{new Products("k6580", 288, "yes")}
};
for (int i = 0; i < names.length; i++) {
for (int j = 0; j < names[i].length; j++) {
String available = names[i][j].getAvailable();
if (available.equals("no")) {
total = total + names[i][j].getPrice();
}
}
System.out.println("");
}
// from here I am not able to find average
double average = total / names.length;
System.out.println("average of price :" + average);
}
}
You need to define a count
variable which to increment it each time according to your if condition ( available.equals("no")
) to just count the element which meet the condition:
double total = 0;
Products[][] names = new Products[][] { { new Products("k7580", 6500, "yes") },
{ new Products("k8570", 7333, "no") }, { new Products("k8580", 766, "no") },
{ new Products("k6580", 288, "yes") } };
int count = 0;
for (int i = 0; i < names.length; i++) {
for (int j = 0; j < names[i].length; j++) {
String available = names[i][j].getAvailable();
if (available.equals("no")) {
count++;
total +=names[i][j].getPrice();
}
}
System.out.println("");
}
// from here I am not able to find average
double average = total / count;
System.out.println("average of price :" + average);
If you use Java 8 or higher, you can use it:
public static void main(String[] args) {
Products[][] names = new Products[][]{
{new Products("k7580", 6500, "yes")},
{new Products("k8570", 7333, "no")},
{new Products("k8580", 766, "no")},
{new Products("k6580", 288, "yes")}
};
Arrays.stream(names)
.flatMap(array -> Arrays.stream(array))
.filter(products -> products.getAvailable().equals("no"))
.mapToDouble(Products::getPrice)
.average()
.ifPresent(average -> System.out.println(
"average of price: " + average));
}
Output is:
average of price: 4049.5
As you are seeing, it is concise. I have used stream here. Stream helps to reduce verbosity from code.
You do not need to define a 2d array, just maintain a 1d array of products and maintain a count of eligible items which needs to be considered for average( in your case items which are unavailable)
This code will run in a single loop without the need of nested loops.
public class Test {
public static void main(String[] args) {
double total = 0;
int notAvailableItems = 0;
Products[] names = new Products[]{
new Products("k7580", 6500, "yes"),
new Products("k8570", 7333, "no"),
new Products("k8580", 766, "no"),
new Products("k6580", 288, "yes")};
for (int i = 0; i < names.length; i++) {
Products product = names[i];
if (product.getAvailable().equals("no")) {
total = total + product.getPrice();
notAvailableItems++;
}
}
double average = total / (double) notAvailableItems;
System.out.println("average of price: " + average);
}
}
Output:
average of price: 4049.5
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.