We're a community of 1.1M IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,080,578 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

Loop driving me nuts

I'm using a loop to get user input and compare it; Idk why but on the first iteration of the while it works fine and displays only once. Second run thru I get two prompts, hoping someone can point out where I'm screwing up.

    public static void main(String[] args) {

        Scanner input = new Scanner(System.in);

        // Creates instance of Store and populates it with cakes and cookies
        Store bakery = fillInventory();

        // Creates instance of Customer and asks user to provide input
        Customer ourCustomer = new Customer("TheCustomer", 5.00);

        Cake theCake = null;
        Cookie theCookie = null;

        boolean validChoice = false;
        boolean validQuantity = false;
        String userChoice = null;
        String cakeOrCookie = "";
        int userQuantity = 0;

        System.out.print("Welcome to the Bakery!\n\n");

        while (ourCustomer.getCustomerMoney() > 0.25 ) {
                System.out.print("Would you like a cake or cookie?(-1 to quit): ");
                cakeOrCookie = input.nextLine();

            if (cakeOrCookie.equalsIgnoreCase("cake")) {
                System.out.print("Here is our cake selection:\n");
                bakery.cakeShop();

                validChoice = false;
                System.out.print("Please select one: ");
                userChoice = input.nextLine();
                validChoice = bakery.validCake(userChoice);
                while (validChoice == false) {
                    System.out.print("I didn't understand your selection, please re-renter: ");
                    userChoice = input.nextLine();
                    validChoice = bakery.validCake(userChoice);
                }

                validQuantity = false;
                System.out.print("How many would you like?: ");
                userQuantity = input.nextInt();
                validQuantity = bakery.checkCake(userChoice, userQuantity);
                while (validQuantity == false) {
                    System.out.print("I'm sorry we can't allow that many, please re-enter: ");
                    userQuantity = input.nextInt();
                    validQuantity = bakery.checkCake(userChoice, userQuantity);
                }

                if (validChoice == true && validQuantity == true) {
                    for (Cake index : bakery.cakeList) {
                        if (userChoice.equalsIgnoreCase(index.getCakeName())) {
                            theCake = index;
                        }
                    }
                }

                bakery.buy(ourCustomer, theCake, userQuantity);
                bakery.printReceipt();
            } else if (cakeOrCookie.equalsIgnoreCase("cookie")) {
                System.out.print("Here is our cookie selection:\n");
                bakery.cookieShop();

                validChoice = false;
                System.out.print("Please select one: ");
                userChoice = input.nextLine();
                validChoice = bakery.validCookie(userChoice);
                while (validChoice == false) {
                    System.out.print("I didn't understand your selection, please re-renter: ");
                    userChoice = input.nextLine();
                    validChoice = bakery.validCookie(userChoice);
                }

                validQuantity = false;
                System.out.print("How many would you like?: ");
                userQuantity = input.nextInt();
                validQuantity = bakery.checkCookie(userChoice, userQuantity);
                while (validQuantity == false) {
                    System.out.print("I'm sorry we can't allow that many, please re-enter: ");
                    userQuantity = input.nextInt();
                    validQuantity = bakery.checkCookie(userChoice, userQuantity);
                }

                if (validChoice == true && validQuantity == true) {
                    for (Cookie index : bakery.cookieList) {
                        if (userChoice.equalsIgnoreCase(index.getCookieName())) {
                            theCookie = index;
                        }
                    }
                }

                bakery.buy(ourCustomer, theCookie, userQuantity);
                bakery.printReceipt();
            } else if ( cakeOrCookie.equalsIgnoreCase("-1") ) {
                break;
            }            
        }



        // once the customer is broke and can't afford anything thank them and print final receipt
        System.out.print("Thanks for shopping! Come back when you have more money!\n");
        System.out.print("Total receipt:\n");

    }

    public static Store fillInventory() {
        Store bakery = new Store();

        bakery.createCake("Cake 1", "Flavor 1", 0.50, 15);
        bakery.createCake("Cake 2", "Flavor 2", 0.50, 15);
        bakery.createCake("Cake 3", "Flavor 3", 0.50, 15);

        bakery.createCookie("Cookie 1", "Flavor 1", 0.25, 15);
        bakery.createCookie("Cookie 2", "Flavor 2", 0.25, 15);
        bakery.createCookie("Cookie 3", "Flavor 3", 0.25, 15);

        return bakery;
    }
}

The code isn't the cleanest but I'm teaching myself so give me a break lol

4
Contributors
3
Replies
7 Hours
Discussion Span
1 Year Ago
Last Updated
4
Views
Question
Answered
crownedzero
Light Poster
38 posts since Sep 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

I kinda for the most part get what your code does but, which loops is prompting twice? You have 3 loops. And what is being prompted twice?

devninja
Light Poster
36 posts since Mar 2012
Reputation Points: 2
Solved Threads: 2
Skill Endorsements: 0

Put a load of print statements into your code, printing the values of the key variables at each stage so you can see where it's going wrong.

JamesCherrill
... trying to help
Moderator
8,668 posts since Apr 2008
Reputation Points: 2,636
Solved Threads: 1,479
Skill Endorsements: 33

You are probably having a problem with the Scanner class. Scanner methods can be tricky. The Scanner buffers input and can block waiting for input.

For example if you enter: A word to the wise <PRESS ENTER>
and use next() only "A" is read, and "word to the wise" is left in the buffer.
Your next attempt to get something from Scanner will be to get "word".
nextInt() will fail here.
To clear the buffer, use the nextLine() method.
To test if the next token is an int, use the hasNextInt() method.

Try calling the nextLine() method to clear the lineend from the buffer after using the nextInt() method.

NormR1
Posting Sage
Team Colleague
7,742 posts since Jun 2010
Reputation Points: 1,158
Solved Threads: 793
Skill Endorsements: 16
Question Answered as of 1 Year Ago by JamesCherrill, NormR1 and devninja

This question has already been solved: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page generated in 0.0654 seconds using 2.72MB