0

I am having issues with my nested while loops working. The assignment is to ask the user for input on what item they want to purchase and then ask how many of that item they want. The end result is for the total of all items chosen once they no longer want to purchase items.

Just to mention, I wanted to use an array instead of nested if...else statements for the price of the items but we have not learned arrays yet in class.

The loop pauses after input of 1/2/3/4/5 but will successfully terminate if the input is 00.

import java.util.Scanner;
public class Shopping {
     

    public static void main(String[] args){
 Scanner input = new Scanner(System.in);
 //initialize variables
double total = 0; // grand total of items and their amounts
double itemNum = 0;// corresponding item number
int itemAmount = 0;// quantity of item
double itemPrice =0; //price of selected itemNum



//offer user product menu
System.out.printf("The following items are available to purchase: %n"
        + "Item 1: $101.00%nItem 2: $119.25%nItem 3: $160.75%nItem 4: $203.00"
        + "%nItem 5: $109.12%n " );
System.out.printf("/////////////////%n%n");

//prompt user for item they want to buy
        System.out.println("Enter what item you want to purchase as 1, 2, 3, etc. OR "
        + "enter 00 to quit.");
         itemNum = input.nextDouble();
        while (itemNum != 00){
            if (itemNum == 1){
                itemPrice = 101.00;
            }
            else {
                if (itemNum ==2){
                    itemPrice = 119.25;
                }
                else {
                    if (itemNum ==3){
                        itemPrice = 160.75;
                    }
                    else {
                        if (itemNum ==4) {
                            itemPrice = 203.00;
                        }
                        else {
                            if(itemNum ==4){
                        itemPrice = 109.12;
                    }
                        else {
                                if (itemNum == 5){
                                itemPrice = 109.12;
                                }    
        }
              while (itemNum  <= 5){
        System.out.println("Enter how many of the item you want to purchase OR "
        + "enter 00 to quit.");
        itemAmount = input.nextInt();
        while (itemAmount != 00){
       {
        total = itemNum * itemAmount;
        }
        }
        }
                        }
                    }
                }
            }
        }
        if (itemNum == 00){
            System.out.printf("Your total for your items is: $%.2f%n", total);
    }
}
}
    ```
3
  • 2
    Your code has horrible formatting, you should not add tabs to your else if, they should all be in the same column, fix your code before asking questions or nobody will care. Commented Sep 21, 2020 at 1:45
  • "Just to mention, I wanted to use an array instead of nested if...else statements for the price of the items but we have not learned arrays yet in class." You know this yourself, you want to challenge yourself, don't give up, try googling more, maybe not the question but the concept of array in java and how to use it. Asking question here and getting the answer from here would not really improve your knowledge, you'll get your answer but probably it won't stick as much if you found the answer yourself. Good luck. Commented Sep 21, 2020 at 1:46
  • @HopefullyHelpful According to my lesson on if...else the tabbed option, which is automatically populated by my IDE I'm using, is acceptable as well as the column formatting you are asking for....sorry you don't want to help because of that. Commented Sep 21, 2020 at 1:56

1 Answer 1

1

Oof, where to start.

00 doesn't do what you think it does.

00 is an octal number that... is the exact, precise same value as 0. There is no way for a double to represent the difference between a user entering just 0 or 00. In fact, if the user enters 000.0000, that's still going to end up being just a 0. Your == 00 code 'works', but suggests something that isn't true. Just write == 0. If you want the loop to end when the input is precisely 00 and not if it is anything else, you cannot call .nextDouble(). Call .next(), check the string input, and Double.parseDouble it (once you determine it wasn't 00) to get to a double, if you must.

Furthermore, the number you're asking for is concrete. Entering "I want item #4.591824" makes no sense. So why are you calling .nextDouble()? Call .nextInt().

That's a horrible if/else formatting.

all that you're doing is mapping an item to a price. There are MUCH nicer ways to do this:

// at the top level (as a sibling to your method defs):
private static final Map<Integer, Double> PRICES = Map.of(
    1, 101.00,
    2, 119.25,
    3, 160.75,
    4, 203.00,
    5, 109.12);

// later in a method
double price = PRICES.getOrDefault(itemNum, 0.0);
// price is now the price. or 0.0 if the item wasn't in there.

There. Got rid of the whole ugly mess with one map, which incidentally now is also quite easy to fill off of a data file or a database or whatnot.

If that's a bridge too far for your first steps in java, make a helper method:

public double getPriceForItem(int itemNum) {
    if (itemNum == 1) return 101.00;
    if (itemNum == 2) return 119.25;
    if (itemNum == 3) return 160.75;
    if (itemNum == 4) return 203.00;
    if (itemNum == 5) return 109.12;
}

Your code is unreadable and it already messed you up.

Your code will check if itemNum is 4, and set itemPrice to 203.00. If that's not it, it will check if itemNum is 4, and then set price to 109.12. Which is obviously gobbledygook, and presumably your eyeballs did not see this obvious messup because you've made messy code. That's the cost of messy code, and it should be a good lesson as to why you don't want code your eyeballs can no longer track.

while loops for input

You then start using while loops to ask for a single bit of input (such as 'how many do you want to buy'. That's not what while loops are for.

This code should have exactly 1 while loop. Not 3.

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

2 Comments

Well said, just one minor problem... OP clearly stated that they haven't learned arrays yet in class so I doubt they covered maps. Not that it makes your answer invalid (upvoted), just saying that an answer that uses only primitive data types could be potentially better for the OP even though it is a bit less clean and a bit longer.
I'm hoping they learned methods, so that the second option is workable for them, with the first option showing the general vibe of how 'good' code would do it... assuming i get away with calling my preferred styles 'good' - I would hope most java coders would agree the map solution looks decent and is very flexible.

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.