0

Can someone tell me why I can't get the highest value and minimum of value of multiplying both the price and the quantity of these items?

public class StoreProgram {
    public static void main(String[] args) {
        String[] storeItems = {
                "broccoli", "onion", "carrot", "turnip", "mango",
                "bread", "garlic", "celery", "apple", "banana",
                "raisins", "grapes", "lemon", "orange", "potato"};

        int[] itemQuantities = {
                23, 5, 7, 15, 2,
                13, 13, 8, 20, 30,
                3, 25, 10, 9, 1};

        double[] itemPrices = {
                2.0, 0.89, 0.70, 1.50, 2.99,
                3.45, 1.45, 1.12, 3.99, 0.25,
                4.99, 7.00, 1.75, 1.80, 3.25};

        double max = itemQuantities[0] * itemPrices[0];
        double min = itemQuantities[0] * itemPrices[0];
        for (int i = 1; i < storeItems.length; i++) {
            if (max > itemQuantities[i] * itemPrices[i]) {
                max = itemQuantities[i] * itemPrices[i];
                System.out.println("HIGHEST:\n\tItem: " + storeItems[i]
                        + ",\t" + "Inventory Value: $" + max);
            }
            if (min < itemQuantities[i] * itemPrices[i]) {
                min = itemQuantities[i] * itemPrices[i];
                System.out.println("Lowest:\n\tItem: " + storeItems[i]
                        + ",\t" + "Inventory Value: $" + min);
            }
        }
    }
}

It prints out the following:

HIGHEST: Item: onion, Inventory Value: $4.45
Lowest: Item: apple, Inventory Value: $79.80000000000001
Lowest: Item: grapes, Inventory Value: $175.0
HIGHEST: Item: potato, Inventory Value: $3.25
6
  • 4
    The only mistakes I can see are (1) that < and > are reversed, (2) you've potentially got a floating point arithmetic error, (3) you're displaying output within the loop, instead of at the end of it. That would make your maximum show as the minimum and vice versa, and also possibly show an amount with too many decimal places. It would also show output repeatedly, instead of just once. Are all those things what's happening? Commented Oct 28, 2020 at 6:13
  • @DawoodibnKareem No I wish if it is the decimal lol. It prints out the following: HIGHEST: Item: onion, Inventory Value: $4.45 Lowest: Item: apple, Inventory Value: $79.80000000000001 Lowest: Item: grapes, Inventory Value: $175.0 HIGHEST: Item: potato, Inventory Value: $3.25 Commented Oct 28, 2020 at 6:16
  • 2
    Sounds like what I described. So you've got three separate bugs, to fix individually. Fix up the < and > signs. Move the output lines to outside the loop. Then read up on how to use the BigDecimal class. Commented Oct 28, 2020 at 6:22
  • @DawoodibnKareem, When I move the print statements outside of the loop, the storeItems[i] doesn't work before it's declared in the loop Commented Oct 28, 2020 at 6:27
  • 1
    Chass has very kindly fixed two of the three bugs for you! Commented Oct 28, 2020 at 6:29

1 Answer 1

1

Since you have not provided an expected output I'll just state the logical error and my quick fix.

if (max > itemQuantities[i] * itemPrices[i]) {
    max = itemQuantities[i] * itemPrices[i];
    System.out.println("HIGHEST:\n\tItem: " + storeItems[i]
            + ",\t" + "Inventory Value: $" + max);
}

is wrong since you are comparing the possible highest value and then assign it to the maximum value if the maximum value is higher which doesn't make sense the same logical error goes to min.

Secondly, you are printing out the value every time you change. Since you are asking for the highest and the lowest values there should only be one for each.

This is what i do

public class StoreProgram {
    public static void main(String[] args) {
        String[] storeItems = {
                "broccoli", "onion", "carrot", "turnip", "mango",
                "bread", "garlic", "celery", "apple", "banana",
                "raisins", "grapes", "lemon", "orange", "potato"};
        int[] itemQuantities = {
                23, 5, 7, 15, 2,
                13, 13, 8, 20, 30,
                3, 25, 10, 9, 1};
        double[] itemPrices = {
                2.0, 0.89, 0.70, 1.50, 2.99,
                3.45, 1.45, 1.12, 3.99, 0.25,
                4.99, 7.00, 1.75, 1.80, 3.25};
        double max = itemQuantities[0] * itemPrices[0];
        double min = itemQuantities[0] * itemPrices[0];
        int highindex, lowindex;
        highindex = lowindex = 0;
        for (int i = 1; i < storeItems.length; i++) {
            if (max < itemQuantities[i] * itemPrices[i]) {
                max = itemQuantities[i] * itemPrices[i];
                highindex = i;
            }
            if (min > itemQuantities[i] * itemPrices[i]) {
                min = itemQuantities[i] * itemPrices[i];
                lowindex = i;
            }
        }
        System.out.println("HIGHEST:\n\tItem: " + storeItems[highindex]
                + ",\t" + "Inventory Value: $" + max);
        System.out.println("Lowest:\n\tItem: " + storeItems[lowindex]
                + ",\t" + "Inventory Value: $" + min);
    }
}

Which outputs the following

HIGHEST:
    Item: grapes,   Inventory Value: $175.0
Lowest:
    Item: potato,   Inventory Value: $3.25
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.