File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes My program won't return the correct ammount of change Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "My program won Watch "My program won New topic

My program won't return the correct ammount of change

russell lloyd

Joined: Feb 09, 2005
Posts: 18
First off I would like to say that I have only been programing for about 4 weeks so I'm sorry if this post is abysmally silly. However, the assignment that I am currently working on doesn't seem to be working correctly and I cannot figure out why. It compiles just fine and runs fine but for some reason it will not return what I designed it to return

the problem appears to be with the pennie while statement...

the program will return the correct ammount of change for several values but it often returns the incorrect number of pennies. For instance 15.88 returns 3 pennies while 15.87 returns only 1

PLZZZZZZZZZ help my project is due in like 2 hours and I thought it worked fine but now it's broken. Any help would be greatly appreciated as I have spent several hours trying to troubleshoot it myself.

[ February 09, 2005: Message edited by: russell lloyd ]
[ February 09, 2005: Message edited by: russell lloyd ]
Hung Yee

Joined: Dec 22, 2004
Posts: 18
Things to watch out for when doing floating point arithmetic:
Steven Bell
Ranch Hand

Joined: Dec 29, 2004
Posts: 1071
A quick fix would be to represent the money as int (number of pennies) rather than float (number of dollars) if that is possible.

Other than that, floating point arithmatic is not exact so you would want to use BigDecimal.
russell lloyd

Joined: Feb 09, 2005
Posts: 18
Thank both of you so much my program works now. I think it would have been nice of my teacher to point that out to us but hey she hasn't even taught us how to use while loops yet so I have spent the last 4 hours trying to learn about while loops. Both of you are eternally my heroes for you have saved my grade. THANK YOU THANK YOU THANK YOU
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
If you understand integer arithmetic, you can actually do this whole thing without any while loops at all. Since you've already done your own implementation, I don't feel bad at all showing you another way to do it:

Notice that the variable names I use above all start with lower case. This is a common convention. Usually class names start with upper case. I would strongly suggest that you follow this convention, especially since it will help others here at the Ranch understand your code more quickly.

Perhaps the trickiest part I use in the code above, is the -= operator. This is just a shortcut: "x -= y" is the same as "x = x - y". Also note that every variable above has type int. The division might work differently than you expect. I won't go into details, but if you'd like to run the code and figure out how it works, you'll need to learn about so-called "integer division".

Mostly, I hope this shows that there is often more than one way to tackle a problem. I don't wish to invalidate your attempts at all. Rather I'm just showing a different way of approaching the problem.

Keep Coding!


Java API Documentation
The Java Tutorial
I agree. Here's the link:
subject: My program won't return the correct ammount of change
It's not a secret anymore!