aspose file tools*
The moose likes Beginning Java and the fly likes Question on int value Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Question on int value" Watch "Question on int value" New topic
Author

Question on int value

Melinda Savoy
Ranch Hand

Joined: Jun 21, 2005
Posts: 386

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.

Thanks.
Jason Kwok
Ranch Hand

Joined: Mar 31, 2005
Posts: 126
Hi Melinda,

You have a couple options. You could add some javascript to your JSP page to check the form input value as soon as the focus leaves that input box and generate an alert if a decimal value has been entered. Or if your posting to a servlet, then you could do something simple like:



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 ]
Melinda Savoy
Ranch Hand

Joined: Jun 21, 2005
Posts: 386

Jason,

I'll give it a try. Thanks for the help. I appreciate your reply and time.

Regards.
Ryan Waggoner
Ranch Hand

Joined: Jun 27, 2007
Posts: 75
You may or may not care, but if your using a servlet, you can create a filter to do your validation. That way you dont have to clutter your servlet with little checks.
Mark Vedder
Ranch Hand

Joined: Dec 17, 2003
Posts: 624

As a side note, while doing the server side validation is good and should be done, you can also add some client side validation. Use JavaScript to ensure the user input an integer. The simple regex (regular expression) [\d]+ can be used to ensure that a positive integer is input in a form field.
Peter Chase
Ranch Hand

Joined: Oct 30, 2001
Posts: 1970
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.
Jason Kwok
Ranch Hand

Joined: Mar 31, 2005
Posts: 126
Hi Peter,

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!"

Thanks again Peter!

Jason
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Question on int value