This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
I have a JSP page whereby when a user enters a decimal value for a quantity (ie., 2.5) in a quantity ordered field, I would like to have an exception generated however I'm not sure how to force this type of exception. When I run the value through my java class there is no exception and the rounded down value of this decimal value appears to update my database with a value of 2.
Is there way to do this in java? I am needing to detect a decimal value and then throw an exception if it is.
Any help or direction would be greatly appreciated.
I'm sure there's a much more elegant way of doing this, but obviously I'm not aware of it!
Hope that helps.
[ July 17, 2007: Message edited by: Jason Kwok ] [ July 17, 2007: Message edited by: Jason Kwok ]
Jason, you have helped me to find a gap in my Java knowledge. I didn't know the modulus (%) operator could be applied to floating-point types, but I now know it can. In 'C' language, it can't, which is the reason for my mistake.
However, I would take issue with the comparison of the result of the modulus operation for exact inequality with 0.0. Floating-point operations are not exact, so exact [in]equality comparisons are usually doomed. I have a feeling that this particular case may be a special one, where you may be able to be sure it will work, but it's a bad habit to get into.
Betty Rubble? Well, I would go with Betty... but I'd be thinking of Wilma.
Joined: Mar 31, 2005
Thanks for the praise, I appreciate it! As for the equality, I wasn't too happy with it either, and wasn't at my desk to actually test how well it would actually work.
I think that's why I ended my post with: "I'm sure there's a much more elegant way of doing this, but obviously I'm not aware of it!"