programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Sheriffs:
Saloon Keepers:
Bartenders:

If and Else with <1.0

Ranch Hand
Posts: 234
3
Hi,

My below code. I am having trouble with the setLength. It's supposed to send the value of 0.0 IF the length value is less than 0.0f and 51.0f if it's greater than 50.0f otherwise ( else ) just send whatever the value is. Well when the value is greater than 50.0f it works as expected but when the value is less than 1.0f ( say .10f or .5f) it does not. It's sending 0.0 but instead it sends whatever the value is.

I asked my instructor about this but I don't understand his response to me. He says "If length is -4.0 you'll set lengthVerified to 0.0 but then set it back to -4.0 since -4.0 > 50.0 is false." What does this mean? I'm not using -4.0 and how is -4.0 greater than 50.0? Thank You

Rancher
Posts: 784
19
For one thing you are using doubles and "==", doubles are an approximation and not an exact number. So in your example you are using comparisons of > and < which work fine, if you have a value of myValue < 0, that will happen in a double or floatingpoint math, where myValue == 0 is not as probable and in your myValue > 50 is also very probable where myValue == 50 is far less probable.

So look at your comparisons, they don't make any sense--in your main.

Also in your setLength, you are missing an else on the first if clause.

 It is sorta covered in the JavaRanch Style Guide.