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.

Page 1 of 2 12 LastLast
Results 1 to 25 of 30

Thread: Do While Loop

  1. #1
    Member
    Join Date
    Oct 2012
    Posts
    61
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Do While Loop

    So I am prompting the user for donations in a do while loop while the total is less than 500. I need to print out who the highest donor was, how would I do this?


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Do While Loop

    Have you tried to think how to program this problem? What steps must the code take to do what you want it to do?
    Try making a list of the steps and if you have problems, post the list and the problems you are having describing the steps for the program to take.
    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:

    javapol (February 15th, 2013)

  4. #3
    Member
    Join Date
    Oct 2012
    Posts
    61
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: Do While Loop

    Yes I have thought about it, I have thought about passing the donation amount to another variable but when it loops up and around to repeat the code then wont the value stored in that variable be wiped?

  5. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Do While Loop

    Try making the list of steps the program needs to do to solve the problem. When you have the list of steps, then worry about how to code it.
    If you don't understand my answer, don't ignore it, ask a question.

  6. #5
    Member
    Join Date
    Oct 2012
    Posts
    61
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: Do While Loop

    1.
    - A For Loop to ask the donor's for their donation details (name & amount)
    - donationQuantity Variable to calculate amount of donations.
    - maxDonation Variable to calculate the total of donations.
    while (max donation is less than 500)
    2.
    - print number of donations
    - print who donated the highest ***** (This is the one I am struggling on)

  7. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Do While Loop

    Step 1. should be broken down into separate, very simple steps. Each step should fit on a line.
    For example:
    initialize total and highest
    begin loop
    ask user for name
    read name and save
    ask user for amount
    read amount and save

    what else is done in the loop?

    end loop; continue looping until ....

    This: while (max donation is less than 500) seems to say the loop continues until there is a donation >= 500
    the last two steps look OK:
    -print number of donations
    -print who donated the highest


    --- Update ---

    Thread continued at http://www.javaprogrammingforums.com...lp-please.html
    If you don't understand my answer, don't ignore it, ask a question.

  8. #7
    Member
    Join Date
    Oct 2012
    Posts
    61
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: Do While Loop

    I have got everything working, it's just I am stuck on what else to add so that I can output 'who donated the most'. Im guessing this would require me to add something inside the loop to store each amount, I cant think how :?

  9. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Do While Loop

    who donated the most
    How would you detect if the new donation is more than the current biggest donation?
    Then what?
    If you don't understand my answer, don't ignore it, ask a question.

  10. #9
    Member
    Join Date
    Oct 2012
    Posts
    61
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: Do While Loop

    Oh, with an IF Statement, I think I know now

  11. #10
    Member
    Join Date
    Oct 2012
    Posts
    61
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: Do While Loop

    I don't know the answer to this

  12. #11
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Do While Loop

    Suppose you can determine that the new donation is bigger than the current biggest donation:
    What should be done now that the code has found the new donation is more than the current biggest donation?

    What would you do if you were doing this manually with a piece of paper and I read to you the donations one after the other?
    If you don't understand my answer, don't ignore it, ask a question.

  13. #12
    Member
    Join Date
    Oct 2012
    Posts
    61
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: Do While Loop

    I would do an if statement:

    if(newDonation>currentDonation)
    {
    newdonation=highestdonation
    }

  14. #13
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Do While Loop

    newdonation=highestdonation
    Take a look at that. Does it look correct? It changes the value of newdonation.

    What value is in currentDonation? Is that the correct variable?

    You need to define in English what each of the three variables used in the posted code are supposed to contain.
    If you don't understand my answer, don't ignore it, ask a question.

  15. #14
    Member
    Join Date
    Oct 2012
    Posts
    61
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: Do While Loop

    Thanks for pointing that out, I need to change the values of highest donation not newDonation, therefore, I think the condition should be:
    highestDonation=newDonation
    I have not initialised currentDonation, therefore, it shows an error. I am maybe going the wrong way about this, I appreciate your help in leading me in the right direction.

  16. #15
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Do While Loop

    Have you defined what each of those three variables will hold?
    What's the difference between new and current?
    If you don't understand my answer, don't ignore it, ask a question.

  17. #16
    Member
    Join Date
    Oct 2012
    Posts
    61
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: Do While Loop

    I havent defined current, I don't know what to initialise it as,
    The newDonation is defined under the prompt so that it will store what the user enters.

  18. #17
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Do While Loop

    What data is the currentDonation variable supposed to hold?
    If you don't have any use for it, get rid of it.
    If you don't understand my answer, don't ignore it, ask a question.

  19. #18
    Member
    Join Date
    Oct 2012
    Posts
    61
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: Do While Loop

    I thought id need it for the if statement,

  20. #19
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Do While Loop

    What value will it hold?
    If you don't understand my answer, don't ignore it, ask a question.

  21. #20
    Member
    Join Date
    Oct 2012
    Posts
    61
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: Do While Loop

    Im not 100% sure, so i guess i dont need it.

  22. #21
    Junior Member
    Join Date
    Feb 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Depending on the application of your program, ie practice vs daily, I would suggest using an array.

    Check out the ArrayList type

    I'd make a donation instance variable available to each object, then make an array as a list of those objects (maybe add it to the array through a constructor). Iterate through the array of objects who call to the donation instance variable.

    If obj1.donation > obj2.donation
    highestDonation = obj1

    Until you've iterated all objects.

    But I may be making it more difficult than it needs to be. I always prefer arrays.

  23. #22
    Junior Member
    Join Date
    Feb 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    I tried my method and it was a big whopping fail, haha.

    I'm not sure why I thought creating an object for each person was a good idea, but I feel like just making a multidimensional array, or two separate array lists, would be a better plan.

    One for the person, the other for their donation and make sure indices remain in alignment.

    I'll post my goofball attempt shortly.

  24. #23
    Member
    Join Date
    Feb 2013
    Location
    Canada
    Posts
    54
    Thanks
    0
    Thanked 6 Times in 6 Posts

    Default Re: Do While Loop

    You have a few ways to do this, so start by breaking the problem into small steps. First, you have to get the values of the donations. If you have a lot of donations, it can be very time-consuming and impractical to create a new variable for each. One way around this would be to store each value in an array. Each value should have its own unique index, that is, one value shouldn't override another. Second, after all of them have been entered, you can create a loop that compares two values, stores the higher value under a different variable and keeps going until the array list is completely checked.

    Alternatively, if you're not a fan of arrays, you can use 2 variables and constantly update their values. One variable would store the user's entered value, the other would store the higher of the two values. This approach is a bit tricky because at first, there is only 1 user entered value. You can either compare it with 0 or simply wait for a second value.

    There are probably other ways to approach this problem but these are the first 2 that popped into my head. The type of loop (for, do-while or while) doesn't matter, although based on the title of the thread, I'm guessing you would use a do-while loop.

    Third, you have to ensure the sum of the donations is less than 500 and work this into the loop(s).
    Fourth, something has to happen when the sum is 500. There are many ways to approach this, some which can be done through a single primitive method, whereas others would take more work.

    Each of those four steps have to be broken down further but I'll leave that part up to you.

  25. #24
    Member
    Join Date
    Oct 2012
    Posts
    61
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: Do While Loop

    I have been changing things, trying different things out based on what you guys have said, I am still having trouble. Here is my code
    public class Donations
    {
       public static void main(String[] args)
       {
          Scanner keyboard= new Scanner (System.in);
          String donorName;
          int newDonation, donationQuantity=0, maxDonation=0, highestDonation;
     
          do
          {
             System.out.println("\n");
             System.out.println("Please enter donation amount");        //PRINT
             newDonation=keyboard.nextInt();
     
             System.out.println("Please enter your name");              //PRINT
             donorName=keyboard.next();
     
             highestDonation=newDonation;
     
             if(newDonation>highestDonation)
             {
                highestDonation=newDonation;
             }
     
     
             donationQuantity=donationQuantity+1;
     
             maxDonation +=newDonation;
     
     
          }
          while (maxDonation<500);
     
          System.out.println("It took " + donationQuantity + " donations to reach the £500");
     
          System.out.println("Highest donor was " + highestDonation);
    I know the initialisation of highestDonation is wrong, I don't know how to fix that.

  26. #25
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Do While Loop

     highestDonation=newDonation;
    That sets highestDonation to the value of newDonation changing the old value of highestDonation.
    Then the next statement:
     if(newDonation>highestDonation)
    can never be true because they have the same value from the previous statement.

    how to fix that.
    Remove the first so that highestDonation is only changed if the newDonation is greater.
    If you don't understand my answer, don't ignore it, ask a question.

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

    javapol (February 20th, 2013)

Page 1 of 2 12 LastLast

Similar Threads

  1. For loop, the first command in the loop does not get executed the 2nd time..
    By lina_inverse in forum Loops & Control Statements
    Replies: 1
    Last Post: October 16th, 2012, 09:00 PM
  2. [SOLVED] Please help with my while loop that turned into infinite loop!
    By Hazmat210 in forum What's Wrong With My Code?
    Replies: 9
    Last Post: March 10th, 2012, 11:22 PM
  3. Converting a while loop to a for loop and a for loop to a while loop.
    By awesom in forum Loops & Control Statements
    Replies: 3
    Last Post: February 26th, 2012, 08:57 PM
  4. For loop; Problems with my for loop
    By mingleth in forum Loops & Control Statements
    Replies: 5
    Last Post: November 16th, 2011, 07:24 PM
  5. [SOLVED] My while loop has run into an infinite loop...?
    By kari4848 in forum Loops & Control Statements
    Replies: 3
    Last Post: March 1st, 2011, 12:05 PM