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?
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.
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?
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.
javapol (February 15th, 2013)
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?
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.
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)
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.
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 :?
How would you detect if the new donation is more than the current biggest donation?who donated the most
Then what?
If you don't understand my answer, don't ignore it, ask a question.
Oh, with an IF Statement, I think I know now
I don't know the answer to this
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.
I would do an if statement:
if(newDonation>currentDonation)
{
newdonation=highestdonation
}
Take a look at that. Does it look correct? It changes the value of newdonation.newdonation=highestdonation
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.
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.
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.
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.
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.
I thought id need it for the if statement,
What value will it hold?
If you don't understand my answer, don't ignore it, ask a question.
Im not 100% sure, so i guess i dont need it.
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.
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.
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.
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
I know the initialisation of highestDonation is wrong, I don't know how to fix that.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);
That sets highestDonation to the value of newDonation changing the old value of highestDonation.highestDonation=newDonation;
Then the next statement:can never be true because they have the same value from the previous statement.if(newDonation>highestDonation)
Remove the first so that highestDonation is only changed if the newDonation is greater.how to fix that.
If you don't understand my answer, don't ignore it, ask a question.
javapol (February 20th, 2013)