0
    //different types of items purchased
    System.out.print("How many different types of items are being purchased? " );
    ArraySize = input.nextInt();
    input.nextLine();

    //arrays - being defined after ArraySize
    String[] item = new String[ArraySize];              //each item             
    int[] itemsPurchased = new int[ArraySize];          //item purchased
    double[] price = new double[ArraySize];             //price for each 'line' on the receipt
    double[] itemPrice = new double[ArraySize];         //price of item purchased

    for (int i=0; i<ArraySize; i++){                    //i = variable element counter

    //name of item purchased
    System.out.print("Item purchased: ");
    item[i] = input.nextLine();

    //number of items purchased
    System.out.print("Quantity: ");
    itemsPurchased[i] = input.nextInt();
    input.nextLine();


    //determines price of item based on what was purchased
    if (item.equals("Shoes") || item.equals("shoes") || item.equals("SHOES"))
        itemPrice[i] = 50.00;

    if (item.equals("T-Shirt") || item.equals("t-shirt") || (item.equals("T-SHIRT")))
        itemPrice[i] = 40.00;

    if (item.equals("Shorts") || item.equals("shorts") || item.equals("SHORTS"))
        itemPrice[i] = 75.00;

    if (item.equals("Cap") || item.equals("cap") || item.equals("CAP"))
        itemPrice[i] = 20.00;

    if (item.equals("Jacket") || item.equals("jacket") || item.equals("JACKET"))
        itemPrice[i] = 100.00;

    //adds item and item amount
        price[i] += (itemsPurchased[i] * itemPrice[i]);

    }//end for

I'm trying to make a receipt lines that look something like

Item ---------- Quantity -----------Cost

Item ---------- Quantity -----------Cost

Item ---------- Quantity -----------Cost

but the line I sat up to hold cost (very last line I linked) isn't holding anything after the first element. I only linked what I thought was relevant, I can give the rest of the code if needed.

1
  • 2
    You can use item.equalsIgnoreCase("AnYKIndOfStRiNg)"to not use those 3 equals. Commented Jul 11, 2012 at 2:11

2 Answers 2

1
if (item.equals("Shoes") || item.equals("shoes") || item.equals("SHOES"))

item has the type of String[], it will never be equal to a String. You are testing if a an array of strings is equal to an individual string. This will never return true. You most likely want to be using item[i] instead of just item.

Because of the error above, a value is never assigned to itemPrice[i]. And in turn price[i] will always be 0.

Sign up to request clarification or add additional context in comments.

1 Comment

I'm sorry could you explain again? I've just start using arrays so I'm not entirely sure what I did wrong.
1

All your lines that have: if (item.equals("Shoes") || item.equals("shoes") || item.equals("SHOES"))

should use item[i]

so for example:

if (item[i].equalsIgnoreCase("shoes"))

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.