case 2:
                System.out.println();
                ctr2=1;
                System.out.println("Select products and add to cart");
                for(ctr=0;ctr<nList;ctr++)
                {
                    System.out.println(ctr2 + ". " + itemArray[ctr] + " " + priceArray[ctr]);
                    ctr2++;
                }
                System.out.println();
                System.out.println("Enter the number of item you want to purchase: ");
                int itemPur = Integer.parseInt(a.readLine());
                System.out.println("Choose the product you want to add to cart");
                for(ctr1=0;ctr1<itemPur;ctr1++)
                {
                    System.out.println("Enter your choice: ");
                    choice1=a.readLine();
                    for(ctr=0;ctr<itemPur;ctr++)
                    {
                        if(choice1.equalsIgnoreCase(itemArray[ctr]))
                        cartArray[ctr1]=choice1;
                        ctr1++;
                    }
                }

is this code right for storing choices?

                for(ctr1=0;ctr1<itemPur;ctr1++)
                {
                    System.out.println(cartArray[ctr1] + " " + priceArray[ctr1]);
                    double totalPur = 0;
                    totalPur = totalPur + priceArray[ctr1];
                }
                System.out.println("Your total cost is " + totalPur);
                ctr2=ctr2-ctr2;
                break;

Why does my variable "totalPur" is not recognize if i build the file..
this is just a continuation of the upper part code..

Recommended Answers

All 4 Replies

You declare totalPur inside the loop, so you get a brand new totalPur each time thru the loop and the previous one(s) are forgotten. Declare and initialise it before you enter the loop so the same variable is used throughout.

thanks.. this is the error i get on the lower part code..

Milo 5.5

Bear Brand 49.0

Master Sardines 13.5

Select options to perform:

  1. Adding list of products
  2. Select products and add to cart
  3. Pay products
  4. Display receipts
    Enter your choice:
    2

Select products and add to cart
1. Milo 5.5
2. Bear Brand 49.0
3. Master Sardines 13.5

Enter the number of item you want to purchase:
2
Choose the product you want to add to cart
Enter your choice:
Milo
Milo 5.5
null 49.0
Your total cost is 54.5
Select options to perform:

  1. Adding list of products
  2. Select products and add to cart
  3. Pay products
  4. Display receipts
    Enter your choice:

You'll have to explain exactly how the output differs from what you wanted.

what i want to get is that after i enter the product i want to add to cart.. it will show the item's you added to cart and tells the total amount to be paid..

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.