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.

Simple problem...

Ok, I'm sure this problem is simple but I can't seem to figure it out. I'm basically making a pseudo-vending machine and trying to get the "dime" and "nickel" buttons to work. Whenever a user presses it, it adds to a variable and accrues (and while accrueing, if it reaches a certain number like $0.90 then it allows the button "Water" to be visible)

The issue is that my nickels button (its like them adding a nickel to the machine) allows me to accrue up to the 90cents mark and right on $0.90 it allows the Water button to become visible, but when accrueing using JUST the "dimes" button, it has to wait till it reaches $1.00 then once it says a dollar, it pops up allowing you to see the Water button. If at any time I click the Nickel button (like accrue 0.10 in nickels and then .80 cents in dimes, it makes the Water button visible on .90...)

Re: Simple problem...

I was thinking it had something to do with that too so that was among the first things i tried to use, and it didn't work. (it still waits until I hit $1.00 then displays the "Water" button, when it should display at $.90) I think I may have an error in this section:

Re: Simple problem...

This is a problem with using floating points and truncation errors. The simplest solution is to represent the money amount using integers for cents. You can then get the dollar amounts back using money/100, and use money%100 for the cents amounts.

Re: Simple problem...

One solution is to replace everywhere it states double with integer (as I stated above). You'll need to adjust your math accordingly (increasing the value by 5 for nickels, increasing by 10 for dimes).

Something else you could try is implement the greater than to be at a half cent, since very rarely will you get an error to propagate up that much.