permaculture playing cards*
The moose likes Beginning Java and the fly likes double or float? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "double or float?" Watch "double or float?" New topic

double or float?

Graham Robinson

Joined: Nov 23, 2005
Posts: 16
Hi, this code is basically meant to work out an olympic dive score, Then print out a bar chart, using rectangles. But, the difficulty can be between 1.2 and 3.5. So an Int wont work, but when i change it, there is problems with the rect object, loss of precision or something. And that it needs an int, but of course the bar uses score, which can be anyhing, so that can't be a Int either. I'm really stuck on how to do this, any help would be great thanks.

Heres the code

BTW you may seen things to do with rainfall, i just copied it from an old program, but theyr are just prompts and comments.
[ December 11, 2005: Message edited by: Graham Robinson ]
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
You might try using a scale like 1000 points is the distance between the bottom of the bar chart and the top of the bar chart. Use a double and round it to 2 places, then multiply is by 100 and that will give you a point in the chart.
Graham Robinson

Joined: Nov 23, 2005
Posts: 16
Thanks, but i managed to do it using an int typecast, doesn't give as accurate results, but works.
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
For more accurate results, you might want to try Keith's suggestion. You said that the score can be anything between 1.2 and 3.5. Are you sure about this? The value of PI lies in this range. Is this a possible score value? Somehow I doubt it. In fact, there are probably MANY values between 1.2 and 3.5 that are not likely to be used as a score. However, it might be more helpful to describe what values CAN be used. For example, you can restrict the user input to only a few decimal places (2 for example). In this case, you can use an int that is really 100 times the actual score. This might take some extra processing for getting the input, but it will make drawing the bar chart much easier and more precise.

Alternatively, you can keep the float values that you have and just multiply them by 100 (or 1000 or whatever) before you graph them. The bar chart shows a relative value anyways, so such a scaling won't make any visible difference in the final result. You will still need to cast the result to an int after the multiplication, but it seems you have figured out how to do that.


Java API Documentation
The Java Tutorial
Graham Robinson

Joined: Nov 23, 2005
Posts: 16
Well the scores have to be within that range, as it's the dives difficulty, not the actual score. The bar chart is more of an indication, this is how i did it.

With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
subject: double or float?
Similar Threads
Storing count within a while
Do whiles/Fors and Drawing
ArrayList Exceptions
Preventing program from crashing when user enters a char instead of a numerical input
Drawing in a loop