0

I'm developing a console program that allows you to build your own pizza. I'm getting unexpected output when I run my code. I'm expecting it to print the menu, accept user input for topping choice, ask if the user wants to continue adding toppings and repeat. However this is the output I'm getting.

No key was pressed between highlighted section

Console Output

I suspected it has something to do with my last else statement: else {System.out.println("You did not enter a valid topping choice");}

However removing that did not fix the issue.

Does anyone have any insight as to what is causing the menu to be printed twice after each selection?

Here is my code:


package com.mycompany.pizzapizza;

import java.util.Scanner;

public class test {
    
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        //variable for keeping track of how many toppings are selected
        int maxToppings = 0;
        System.out.println("---------------------");
        System.out.println("Build Your Own Pizza");
        System.out.println("---------------------");
        
        //selecting the crust
        String crustType= "";     
        boolean status = true;
        while(status)  {

            System.out.println("\nChoose your crust:");
            System.out.println("type R for Regular | type C for Cauliflower");
            crustType = sc.nextLine();
            
            if (crustType.equalsIgnoreCase("R")) {
                crustType = "Regular Crust";
                maxToppings++;
                status = false;
                
            }
            else if (crustType.equalsIgnoreCase("C")) {
                crustType = "Cauliflower Crust";
                maxToppings++;
                status = false;
            }
            else {
                System.out.println("Please enter a valid crust");
                status = true;
            }
        }
        
        System.out.println("\n***********************************************");

        //selecting the sauce
        String sauceType="";
        String sauceAmount="";
        boolean status1 = true;
        while(status1)  {
            
            System.out.println("\nPlease Choose One Sauce Option");
            System.out.println("Type Y for Red Sauce, or N for No Red Sauce.");
            sauceType = sc.nextLine();

            if (sauceType.equalsIgnoreCase("Y")) {
                maxToppings++;
                System.out.println("A.  1/4cup     B. 1/2cup");
                sauceAmount = sc.nextLine();
                if (sauceAmount.equalsIgnoreCase("A")) {
                    sauceAmount = "1/4 cup"; 
                 }           
                else if (sauceAmount.equalsIgnoreCase("B")) {
                    sauceAmount= "1/2 cup";            
                }
                sauceType = "Red Sauce";
                status1 = false;
            }
            else if (sauceType.equalsIgnoreCase("N")) {
                sauceAmount = "";
                status1 = false;
            }
            else {
                System.out.println("Please enter a valid sauce option");
                status1 = true;
            }
            
        }
        
        //Adding cheese
        String cheese="";
        String cheeseAmount="";
        boolean status2 = true;
        while(status2)  {
            
            System.out.println("\nWould you like to add cheese?");
            System.out.println("Type Y for cheese, or N for No Cheese");
            cheese = sc.nextLine();

            if (cheese.equalsIgnoreCase("Y")) {
                System.out.println("A.  1/4cup     B. 1/2cup");
                cheeseAmount = sc.nextLine();
                if (cheeseAmount.equalsIgnoreCase("A")) {
                    cheeseAmount = "1/4 cup";
                    maxToppings++;
                 }
                else if (cheeseAmount.equalsIgnoreCase("B")) {
                   cheeseAmount= "1/2 cup";
                   maxToppings++;
                }
                cheese = "Cheese";
                status2 = false;
            }
            else if (cheese.equalsIgnoreCase("N")) {
                cheese="";
                cheeseAmount = "";
                status2 = false;
            }
            else {
                System.out.println("Please enter a valid cheese option");
                status2 = true;
            }
            
        }
        
        System.out.println("\n***********************************************");
       
        //\\//\\//\\//\\//\\//\\PRINT TOPPINGS//\\//\\//\\//\\//\\//\\
        char userChar = 'y';
        String[] toppings = {"Diced onion", "Green pepper", "Pepperoni", "Sliced mushrooms", "Jalapenos", "Pineapple", "Dry red pepper", "Basil", "Tofu", "Sardines", "Ham"};
        String[] choices = { "A. ", "B. ", "C. ", "D. ", "E. ", "F. ", "G. ", "H. ", "I. ", "J. ", "K. "};
        boolean[] toppingSelected = { false, false, false, false, false, false, false, false, false, false, false};
        
        //\\//\\//\\//\\//\\//\\Enter topping menus loop//\\//\\//\\//\\//\\//\\
    while (userChar == 'y') {
            
        if (maxToppings < 8) {
                System.out.println("\nChoose up to 5 additional toppings.");
           
            

            


                for (int i=0; i<choices.length; ++i) {
                    if(toppingSelected[i] == false) {
                    System.out.println(choices[i] + toppings[i]);  
                    }    
                }
            
            
            
        //\\//\\//\\//\\//\\//\\CHOOSE TOPPINGS//\\//\\//\\//\\//\\//\\
        
          System.out.println("\nEnter a choice: ");
            String userToppings = sc.nextLine();
            //Diced onion
            if (userToppings.equalsIgnoreCase("A")) {
               
                System.out.println("Please choose one amount: ");
                System.out.println("a. 1/8 cup                 b. 1/4 cup");
                userToppings = sc.nextLine();
                if (userToppings.equalsIgnoreCase("A")) {
                    choices[0]="1/8 cup";
                     maxToppings++;
                }
                else {
                    choices[0]="1/4 cup";
                     maxToppings++;
                }
                toppingSelected[0]= true;
                System.out.println("Would you like to add more toppings? Type Y for yes or any other key to submit your recipe.");
                userChar = sc.next().charAt(0);
            }
            
            //Green pepper
            else if (userToppings.equalsIgnoreCase("B")) {
                
                System.out.println("Please choose one amount: ");
                System.out.println("a. 1/8 cup                 b. 1/4 cup");
                userToppings = sc.nextLine();
                if (userToppings.equalsIgnoreCase("A")) {
                    choices[1]="1/8 cup";
                    maxToppings++;
                }
                else {
                    choices[1]="1/4 cup";
                    maxToppings++;
                }
                toppingSelected[1]= true;
                System.out.println("Would you like to add more toppings? Type Y for yes or any other key to submit your recipe.");
                userChar = sc.next().charAt(0);
            }
            
            //Pepperoni
            else if (userToppings.equalsIgnoreCase("C")) {
                System.out.println("Please choose one amount: ");
                System.out.println("a. 2 pieces     b. 4 pieces     c. 6 pieces     b. 8 pieces");
                userToppings = sc.nextLine();
                if (userToppings.equalsIgnoreCase("A")) {
                    choices[2]="2 pieces";
                    maxToppings++;
                }
                else if (userToppings.equalsIgnoreCase("B")) {
                    choices[2]="4 pieces";
                    maxToppings++;
                }
                else if (userToppings.equalsIgnoreCase("C")) {
                    choices[2]="6 pieces";
                    maxToppings++;
                }
                else if (userToppings.equalsIgnoreCase("D")) {
                    choices[2]="8 pieces";
                    maxToppings++;
                }   
                toppingSelected[2]= true;
                System.out.println("Would you like to add more toppings? Type Y for yes or any other key to submit your recipe.");
                userChar = sc.next().charAt(0);
            }
            
            //Sliced mushrooms
            else if (userToppings.equalsIgnoreCase("D")) {
                System.out.println("Please choose one amount: ");
                System.out.println("a. 1/8 cup                 b. 1/4 cup");
                userToppings = sc.nextLine();
                if (userToppings.equalsIgnoreCase("A")) {
                    choices[3]="1/8 cup";
                    maxToppings++;
                }
                else {
                    choices[3]="1/4 cup";
                    maxToppings++;
                }
                toppingSelected[3]= true;
                System.out.println("Would you like to add more toppings? Type Y for yes or any other key to submit your recipe.");
                userChar = sc.next().charAt(0);
            }
            
            //Jalapenos
            else if (userToppings.equalsIgnoreCase("E")) {
                System.out.println("Please choose one amount: ");
                System.out.println("a. 1/8 cup                 b. 1/4 cup");
                userToppings = sc.nextLine();
                if (userToppings.equalsIgnoreCase("A")) {
                    choices[4]="1/8 cup";
                    maxToppings++;
                }
                else {
                    choices[4]="1/4 cup";
                    maxToppings++;
                }
                toppingSelected[4]= true;
                System.out.println("Would you like to add more toppings? Type Y for yes or any other key to submit your recipe.");
                userChar = sc.next().charAt(0);
            }
            
            
            //Pineapple
            else if (userToppings.equalsIgnoreCase("F")) {
                System.out.println("Please choose one amount: ");
                System.out.println("a. 2 pieces     b. 4 pieces     c. 6 pieces     d. 8 pieces");
                userToppings = sc.nextLine();
                if (userToppings.equalsIgnoreCase("A")) {
                    choices[5]="2 pieces";
                    maxToppings++;
                }
                else if (userToppings.equalsIgnoreCase("B")) {
                    choices[5]="4 pieces";
                    maxToppings++;
                }
                else if (userToppings.equalsIgnoreCase("C")) {
                    choices[5]="6 pieces";
                    maxToppings++;
                }
                else if (userToppings.equalsIgnoreCase("D")) {
                    choices[5]="8 pieces";
                    maxToppings++;
                }   
                toppingSelected[5]= true;
                System.out.println("Would you like to add more toppings? Type Y for yes or any other key to submit your recipe.");
                userChar = sc.next().charAt(0);
            }
            
            //Dry red pepper
            else if (userToppings.equalsIgnoreCase("G")) {
                System.out.println("Please choose one amount: ");
                System.out.println("a. 1 Generous Sprinkle   b. 2 Generous Sprinkle   c. 3 Generous Sprinkle   d. 2 Generous Sprinkle");
                userToppings = sc.nextLine();
                if (userToppings.equalsIgnoreCase("A")) {
                    choices[6]="1 Generous Sprinkle";
                    maxToppings++;
                }
                else if (userToppings.equalsIgnoreCase("B")){
                    choices[6]="2 Generous Sprinkle";
                    maxToppings++;
                }
                else if (userToppings.equalsIgnoreCase("C")){
                    choices[6]="3 Generous Sprinkle";
                    maxToppings++;
                }
                else if (userToppings.equalsIgnoreCase("D")){
                    choices[6]="4 Generous Sprinkle";
                    maxToppings++;
                }
                toppingSelected[6]= true;
                System.out.println("Would you like to add more toppings? Type Y for yes or any other key to submit your recipe.");
                userChar = sc.next().charAt(0);
            }
            //Basil
            else if (userToppings.equalsIgnoreCase("H")) {
                System.out.println("Please choose one amount: ");
                System.out.println("a. 1 Generous Sprinkle   b. 2 Generous Sprinkle");
                userToppings = sc.nextLine();
                if (userToppings.equalsIgnoreCase("A")) {
                    choices[7]="1 Generous Sprinkle";
                    maxToppings++;
                }
                else if (userToppings.equalsIgnoreCase("B")){
                    choices[7]="2 Generous Sprinkle";
                    maxToppings++;
                }
                toppingSelected[7]= true;
                System.out.println("Would you like to add more toppings? Type Y for yes or any other key to submit your recipe.");
                userChar = sc.next().charAt(0);
            }
            //Tofu
            else if (userToppings.equalsIgnoreCase("I")) {
                System.out.println("Please choose one amount: ");
                System.out.println("a. 1/4 cup                 b. 1/2 cup");
                userToppings = sc.nextLine();
                if (userToppings.equalsIgnoreCase("A")) {
                    choices[8]="1/4 cup";
                    maxToppings++;
                }
                else {
                    choices[8]="1/2 cup";
                    maxToppings++;
                }
                toppingSelected[8]= true;
                System.out.println("Would you like to add more toppings? Type Y for yes or any other key to submit your recipe.");
                userChar = sc.next().charAt(0);
            }
            //Sardines
            else if (userToppings.equalsIgnoreCase("J")) {
                System.out.println("Sorry we're all out of sardines.");
                toppingSelected[9]= false;
                System.out.println("Would you like to add more toppings? Type Y for yes or any other key to submit your recipe.");
                userChar = sc.next().charAt(0);
            }
            //Ham
            else if (userToppings.equalsIgnoreCase("K")) {
                System.out.println("Please choose one amount: ");
                System.out.println("a. 4 pieces     b. 8 pieces     c. 12 pieces     d. 16 pieces");
                userToppings = sc.nextLine();
                if (userToppings.equalsIgnoreCase("A")) {
                    choices[10]="4 pieces";
                    maxToppings++;
                }
                else if (userToppings.equalsIgnoreCase("B")) {
                    choices[10]="8 pieces";
                    maxToppings++;
                }
                else if (userToppings.equalsIgnoreCase("C")) {
                    choices[10]="12 pieces";
                    maxToppings++;
                }
                else if (userToppings.equalsIgnoreCase("D")) {
                    choices[10]="16 pieces";
                    maxToppings++;
                }   
                toppingSelected[10]= true;
                System.out.println("Would you like to add more toppings? Type Y for yes or any other key to submit your recipe.");
                userChar = sc.next().charAt(0);
            }
            
            /*else if (userChar!= 'z') {
                System.out.println("You did not enter a valid topping choice");
            }*/
        }
        else {
            userChar = 'n';
            System.out.println("You have reached maximum");
        }

            
        
    }
        System.out.println("\n***********************************************");

        //\\//\\//\\//\\//\\//\\PRINT RECIPE//\\//\\//\\//\\//\\//\\
        System.out.println("maxToppings: " + maxToppings);
        System.out.println("Your pizza recipe is: ");
        System.out.printf("\n%-19s %-9s",crustType, "1");
        System.out.printf("\n%-19s %-9s",sauceType, sauceAmount);
        System.out.printf("\n%-19s %-9s",cheese, cheeseAmount);
        for (int i=0; i<toppings.length; i++) {
            if (toppingSelected[i] == true) {
            System.out.printf("\n%-19s %-9s",toppings[i], choices[i]);  
            }
        }
        System.out.println("\nYour pizza will be cooked to perfection.");        
    }
}

L3i5ur3L3
  • 21
  • 1
  • 1
    `userChar = sc.next().charAt(0);` is the issue. If you use `next()` instead of `nextLine` it will not consume the line terminator so you need a blank `sc.nextLine()` right after to consume the line terminator if you plan on using `nextLine()` later in the program. Or you use `sc.nextLine.charAt(0)` and you won't run into the issue. – Nexevis Dec 01 '22 at 19:26

0 Answers0