0

In my code, I am trying to allow the customer to order cheesecake topping and size then give them the price of that cheesecake then add the total to a running total until they finish ordering then give them grand total order at the end when they are finished ordering.

When the code runs it gets to end gives total but no matter whether I hit yes or no on the second time around it immediately goes to cake size instead of letting me choose my flavor again and never ends to let me get my grand total.

    float biteSize = 3;                                                     
            float small = 9;
            float large = 12;
            String chosenSize = "";
            double pricePerInch = 0;
            double total = 0;
            String userFlavor = "";
            String[] chooseSizes = {"bite size", "small", "large"};
            String[] chooseFlavors = {"plain", "strawberry", "raspberry", "caramel", "chocolate"};
            boolean flavorFound = false;
            String want = "";
            double newTotal = 0;

            Scanner scnr = new Scanner(System.in);          

            System.out.println("Flavors to choose from: plain, strawberry, raspberry, caramel, chocolate.");        //giving user flavor list


        while (want != "n") {

            while (!flavorFound) {                      

              System.out.println("Please choose flavor:");                                  // Loop until flavorFound is true

              userFlavor = scnr.nextLine();

              for(int i = 0; i < chooseFlavors.length; i++) {                               // Loop from 0 to choosFlavors.length

                  if(userFlavor.equals(chooseFlavors[i])) {                                 // Compare user input to flavor at index i

                      System.out.println(userFlavor);               

                      flavorFound = true; 
                                                                                            // This is the flag to break out of while loop
                      break;
                                                                                            // Only breaks out of for loop
                  } else {

                      System.out.println("Please choose from flavors above.");
                  }
              }

            }



            if (userFlavor.contains("plain")) {                                         // setting prices for all the flavors

                pricePerInch = 0.50;

            }
            else if (userFlavor.contains("strawberry")) {
                pricePerInch = 1.25;
            }
            else if (userFlavor.contains("raspberry")) {
                pricePerInch = 1.15;
            }
            else if (userFlavor.contains("caramel")) {
                pricePerInch = 0.75;
            }
            else if (userFlavor.contains("chocolate")) {
                pricePerInch = 0.85;
            }

            System.out.println("Sizes to choose from: bite size, small, large");


            boolean sizeFound = false;

            while (!sizeFound) {            
                                                                                            // Loop until flavorFound is true
              System.out.println("Please choose cheese cake size: ");   

              chosenSize = scnr.nextLine(); 

              for(int i = 0; i < chooseSizes.length; i++) {     
                                                                                            // Loop from 0 to choosFlavors.length
                  if(chosenSize.equals(chooseSizes[i])) { 
                                                                                            // Compare user input to flavor at index i
                      System.out.println(chosenSize);

                      sizeFound = true;         
                                                                                            // This is the flag to break out of while loop
                      break;        
                                                                                            // Only breaks out of for loop
                  } else {

                      System.out.println("Please choose from size above.");
                  }
              }

            }

            //chosenSize = scnr.nextLine();


            if (chosenSize.contains("bite size")) {                                         //setting the prices for the sizes

                total = pricePerInch * biteSize;

            }
            else if (chosenSize.contains("small")) {

                total = pricePerInch * small;

            }
            else if (chosenSize.contains("large")) {

                total = pricePerInch * large;

            }



            System.out.println("Your chosen flavor:  " + userFlavor);                                   /*printing out a receipt for the customer with
                                                                                                      the size, toping, and total cost*/    
            System.out.println("Your chosen size: " + chosenSize);

            System.out.println("Price for topping: " + "$" + pricePerInch + " per inch.");

            System.out.println("Size of cheesecake: " + chosenSize + " inches.");

            System.out.printf("Your total cost will be: $" +  newTotal + " dollars.");

            newTotal = total + newTotal;

System.out.println("Would you like to order a cheesecake please choose y/n");

            want = scnr.next(); 
        }

            System.out.printf("Your grand total cost will be: $" +  newTotal + " dollars.");



        }

    }
David
  • 65
  • 5
  • 3
    Looking through your recent questions David, I'd highly recommend that you don't solve your homework by posting every problem you run into on stack overflow. It just isn't the right place for that. Instead use the rest of the internet as a resource to really understand the code you're writing. For instance, what the Scanner class really does and how it's various methods work... – kay Jul 10 '18 at 00:45
  • Also, note that objects (including strings) should always be compared using the `equals` function, not `==`. You used == at least once, in `want != "n"`. See https://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java. That's probably causing all least one of your problems. – yshavit Jul 10 '18 at 01:57
  • Yes, thank you yshavit. That was causing the while loop not ending correctly. I figured out my other problem was my boolean flavorFound = false; was in the incorrect spot outside of the while loop so once changing it to true it would not change it back to false for the next run and would skip the flavor part of the loop. – David Jul 10 '18 at 02:05
  • The question shouldn't be asked at the end of the while loop? newTotal = total + newTotal; *System.out.println("Would you like to order a cheesecake please choose y/n"); want = scnr.next();* } System.out.printf("Your grand total cost will be: $" + newTotal + " dollars."); – JPPat Jul 10 '18 at 00:36
  • not necessarily ... – kay Jul 10 '18 at 00:38
  • Sorry, but it doesn't make sense to ask at the begining of the loop (as if it was a DO-WHILE), the code won't check the answer the first iteration and therefore will run the whole code. I would add a break if the answer is no, if you still want to go that way... – JPPat Jul 10 '18 at 00:50
  • If you read through the OP's code, you'd realize that it is his very intention to loop through the code once and then ask if they would like to order a cheesecake again. You are correct that a do while loop would be better in this scenario but that doesn't solve the OP's problem – kay Jul 10 '18 at 00:54

1 Answers1

2

change

want = scnr.next();

to

want = scnr.nextLine();

I'd recommend taking a look at this question:

What's the difference between next() and nextLine() methods from Scanner class?

kay
  • 371
  • 4
  • 18
  • thank you for the advice. I looked at the post you recommended as well as changed that part in my code but it doesn't fix the issue of moving to the middle of the while loop instead of back to the top and after researching that I cant find a reason why it is picking that specific spot to return to. – David Jul 10 '18 at 00:56
  • 1
    Another thing that is specific to Java (and which might affect what you're having problems with) is how Strings are compared in Java. Instead of using `!=, ==` etc., you need to use `.equals()`. So, `while (want != "n")` should probably be `while (!want.equals("n"))` instead. – Mick Mnemonic Jul 10 '18 at 01:08
  • Thank you, Mick, that fixed my problem of it not ending correctly but I still can't figure out why it is looping back to the middle of code at the choosing size part instead of back to the beginning of while loop for the flavors as well. – David Jul 10 '18 at 01:22