Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: Trouble with a "do while loop."

  1. #1
    Junior Member
    Join Date
    Nov 2023
    Posts
    5
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Trouble with a "do while loop."

    (Apologies if this is in the wrong forum topic - is this post better for a different version of Java? If so, which one?) I'm struggling to learn Java, so I made myself an exercise to play around with in learning conditional statements. When I run this code, and it gets to line 70, it duplicates the part that asks if the user wants to take anymore fruits. I haven't been able to figure out what to do to make it only ask that once. How do I get this thing to stop asking that question twice before waiting for the user to give a "yes" or "no" answer?

    Here's the entire code:

     
    // Import the Scanner class for user input
    import java.util.Scanner;
     
    public class Main {
     
        public static void main(String[] args) {
            // Create a Scanner object for user input
            Scanner scanner = new Scanner(System.in);
     
            // Get the number of apples, oranges, and bananas
            System.out.println("Enter the number of apples in the basket: ");
            int appleCount = scanner.nextInt();
     
            System.out.println("Enter the number of oranges in the basket: ");
            int orangeCount = scanner.nextInt();
     
            System.out.println("Enter the number of bananas in the basket: ");
            int bananaCount = scanner.nextInt();
     
            // Check if there are enough apples
            if (appleCount > 0) {
                System.out.println("There are " + appleCount + " apples available.");
                System.out.println("How many apples do you want to take? ");
                int applesToTake = scanner.nextInt();
     
                if (applesToTake <= appleCount) {
                    appleCount -= applesToTake;
                    System.out.println("You took " + applesToTake + " apples.");
                } else {
                    System.out.println("Not enough apples. You can only take " + appleCount + " apples.");
                }
            } else {
                System.out.println("There are no apples in the basket.");
            }
     
            // Check if there are enough oranges
            if (orangeCount > 0) {
                System.out.println("There are " + orangeCount + " oranges available.");
                System.out.println("How many oranges do you want to take? ");
                int orangesToTake = scanner.nextInt();
     
                if (orangesToTake <= orangeCount) {
                    orangeCount -= orangesToTake;
                    System.out.println("You took " + orangesToTake + " oranges.");
                } else {
                    System.out.println("Not enough oranges. You can only take " + orangeCount + " oranges.");
                }
            } else {
                System.out.println("There are no oranges in the basket.");
            }
     
            // Check if there are enough bananas
            if (bananaCount > 0) {
                System.out.println("There are " + bananaCount + " bananas available.");
                System.out.println("How many bananas do you want to take? ");
                int bananasToTake = scanner.nextInt();
     
                if (bananasToTake <= bananaCount) {
                    bananaCount -= bananasToTake;
                    System.out.println("You took " + bananasToTake + " bananas.");
                } else {
                    System.out.println("Not enough bananas. You can only take " + bananaCount + " bananas.");
                }
            } else {
                System.out.println("There are no bananas in the basket.");
            }
     
            // Ask if the user wants to take any more fruits
            String choice;
            do {
                System.out.println("Do you want to take any more fruits? (Enter 'yes' or 'no')");
                choice = scanner.nextLine();
            } while (!choice.equalsIgnoreCase("yes") && !choice.equalsIgnoreCase("no"));
     
            while (choice.equalsIgnoreCase("yes")) {
                // Check if there are any apples left
                if (appleCount > 0) {
                    System.out.println("There are " + appleCount + " apples left.");
                    System.out.println("Do you want to take any more apples? (Enter 'yes' or 'no')");
                    choice = scanner.nextLine();
     
                    if (choice.equalsIgnoreCase("yes")) {
                        System.out.println("How many more apples do you want to take? ");
                        int applesToTake = scanner.nextInt();
     
                        if (applesToTake <= appleCount) {
                            appleCount -= applesToTake;
                            System.out.println("You took " + applesToTake + " more apples.");
     
                            if (orangeCount > 0) {
                                System.out.println("There are " + orangeCount + " oranges left.");
                                System.out.println("Do you want to take any more oranges? (Enter 'yes' or 'no')");
                                choice = scanner.nextLine();
     
                                if (choice.equalsIgnoreCase("yes")) {
                                    System.out.println("How many more oranges do you want to take? ");
                                    int orangesToTake = scanner.nextInt();
     
                                    if (orangesToTake <= orangeCount) {
                                        orangeCount -= orangesToTake;
                                        System.out.println("You took " + orangesToTake + " more oranges.");
                                    } else {
                                        System.out.println("There are no more oranges left.");
                                    }
     
                                    if (bananaCount > 0) {
                                        System.out.println("There are " + bananaCount + " bananas left.");
                                        System.out.println("Do you want to take any more bananas? (Enter 'yes' or 'no')");
                                        choice = scanner.nextLine();
     
                                        if (choice.equalsIgnoreCase("yes")) {
                                            System.out.println("How many more bananas do you want to take? ");
                                            int bananasToTake = scanner.nextInt();
     
                                            if (bananasToTake <= bananaCount) {
                                                bananaCount -= bananasToTake;
                                                System.out.println("You took " + bananasToTake + " more bananas.");
                                            } else {
                                                System.out.println("There are no more bananas left.");
                                            }
     
                                        }
                                    }
                                }
                            }
                        }
                    }
                    // Thank the user and close the scanner object
                    System.out.println("Thank you and have a nice day!");
                    scanner.close();
                }
            }
        }
    }
    Last edited by ChewyGrizzy; November 21st, 2023 at 10:44 PM. Reason: Not sure where to put this post after all. Newb over here.

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Trouble with a "do while loop."

    The problem might be using the Scanner class's nextLine after using the nextInt method. nextInt leaves the line-end character in the Scanner's buffer and then the call to nextLine reads that line-end and gets an empty String. Call nextLine a second time to get the desired data.
    See this link:
    https://www.geeksforgeeks.org/why-is...ext-functions/
    If you don't understand my answer, don't ignore it, ask a question.

  3. The Following User Says Thank You to Norm For This Useful Post:

    ChewyGrizzy (November 23rd, 2023)

  4. #3
    Junior Member
    Join Date
    Nov 2023
    Posts
    5
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Thumbs up Re: Trouble with a "do while loop."

    Awesome! It worked! Thanks!

    Now I have to try to figure out why it worked. (Why would Java interpret the scanner.nextLine() that way after the several nextInt methods above it were used?) Along that vein, I have some follow up questions so I can try to understand what happened here better (and some posting etiquette questions as well).

    Are you saying that the nextInt methods put the numbers of each fruit in, but that first nextLine afterwards clears the buffer (or memory or whatever it is the info is stored in) of everything but the numbers of each fruit, and that clearing includes any possibility of the user answering with a "yes" or "no" and so it just repeats the question and that time it waits for an answer, but adding the new nextLine method starts the process over again after the first time the question is asked, so the user has a chance to answer with "yes" or "no"? Am I at least close in correctly interpreting what you were trying to tell me?

    I did read the page you linked me to, but I'm still not totally "getting" it yet. It looks like they just converted all the nextInt methods to nextLine instead. Is that a better approach to Java programming in general?

    For posterity's sake, here's what I wrote in the applicable section of the code, that solved the problem, as you mentioned it would:

    do {
                System.out.println("Do you want to take any more fruits? (Enter 'yes' or 'no')");
                choice = scanner.nextLine();
                choice = scanner.nextLine(); // Here's where I added the second choice = scanner.nextLine();

    Also, the etiquette questions about where I should have posted this question:

    Should I have posted it in a different forum on this site, not the Java SE forum? What I'm doing is just straight, basic Java after I installed it on my Windows 10 machine, from Oracle, off their main Java homepage. It's not anything specialized or anything (is what I downloaded actually also known as Java SE too?). If applicable, where would it have been better to post it (so I can know in the future if I need to post about these kinds of things again)?

  5. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Trouble with a "do while loop."

    You posted correctly. This is the section for code with problems.

    I don't know why the designers made the Scanner class work the way it does. It is a problem for everyone.
    If you don't understand my answer, don't ignore it, ask a question.

  6. The Following User Says Thank You to Norm For This Useful Post:

    ChewyGrizzy (November 23rd, 2023)

  7. #5
    Junior Member
    Join Date
    Nov 2023
    Posts
    5
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Thumbs up Re: Trouble with a "do while loop."

    Thank you for letting me know that I put this question in the right place, and that the scanner issue is a problem for everyone! Do the designers ever modify Java and give updates, or is it an "as is" programming language that never changes? Should I look forward to bug fixes or whatever?

  8. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Trouble with a "do while loop."

    Java has made lots of changes over the years and continues to make changes by adding new features.
    See this: https://docs.oracle.com/en/java/java...3-41031E5CD094
    If you don't understand my answer, don't ignore it, ask a question.

  9. The Following User Says Thank You to Norm For This Useful Post:

    ChewyGrizzy (November 24th, 2023)

  10. #7
    Junior Member
    Join Date
    Nov 2023
    Posts
    5
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Trouble with a "do while loop."

    Thank you! I've bookmarked that link and will review it from time to time! I'm glad they do updates. I hope they'll work this bug out! I really appreciate your taking the time to help me understand so much! Hope you had a great Thanksgiving!

Similar Threads

  1. Replies: 1
    Last Post: August 20th, 2019, 07:07 AM
  2. Replies: 4
    Last Post: July 18th, 2014, 02:04 AM
  3. Replies: 1
    Last Post: July 16th, 2014, 04:16 AM
  4. Replies: 2
    Last Post: May 22nd, 2014, 01:17 PM
  5. Trying to get an "if" statement to work in a "for loop".
    By JAKATAK in forum What's Wrong With My Code?
    Replies: 4
    Last Post: April 1st, 2013, 11:16 PM

Tags for this Thread