Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Question on int value

 
Melinda Savoy
Ranch Hand
Posts: 387
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 126
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 387
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jason,

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

Regards.
 
Ryan Waggoner
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 624
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Jason Kwok
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic