Only 48 hours left in the trailboss' kickstarter!

New rewards and stretch goals. CLICK HERE!



  • Post Reply Bookmark Topic Watch Topic
  • New Topic

TagLibrary /NumberFormatException and Form......  RSS feed

 
Gabriel Fox
Ranch Hand
Posts: 170
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Guys...
1.How do i handle retrieving form variable which are numbers
say (int or double primitive type),
I used :
int xx=Integer.parseInt(request.getParameter("fieldName"));
--line1
And i confirmed an exception was being throw which prevents the
statement after line 1 from being reached.
2. Does Tomcat 3.2.1 support the use of Tag Libraries.
Thanks.
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13078
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1. Just catch the NumberFormatException and provide an error message or a default value.
2. Sure - as I recall an example is part of the distribution. However, you should seriously consider Tomcat 4 for the latest JSP implementation.
Bill
------------------
author of:
 
Tim Holloway
Bartender
Posts: 18531
61
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The HTTP protocol is text-based, so there's no such thing as an "int" or "double" form variable -- they're always strings.
Your application may, of course, need something more suitable for calculations, and therefore you use the parseInt, etc. methods.
To do this safely, first you should make sure that the parameter was actually sent! If there's a fuzzy spot or bug in the client side, or if a person should accidently or maliciously send a badly-formed request, you'll get back <i>null</i> instead of a string. So first, make sure you really GOT the number!
To further prevent blowups, encapsulate the conversion in a try/catch block:
 
Gabriel Fox
Ranch Hand
Posts: 170
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Thanks Tim for your prompt reply.I actually added the try-catch block initially.And the exception was caught.Just wondering why
i should have this exception when i am not entering any strange
text in my form.
1. What causes this NumberFormatException i donot really understand?
2.How can one prevent its occurence to avoid break in program
flow?
Thanks a lot.
 
Abhinav Sharma
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi there,
I totally agree with tim. Actually the code which he sent you should use that but instead of typing a specific exception try this code which tim has sent you i am just modifying it. Here goes tim's code with slight modification
String inParm = request.getParameter("fieldName");
if ( inParm == null )
{
// missing parm - throw exception or something
// (maybe set string to a default value)}int inValue = 0;
// default value, scoping outside "try" block
try {
inValue = Integer.parseInt( inParm );}
catch (Exception nfe ) {
// bad number, take default action or throw exception
out.print(nfe);
}
Print the exception in out.print() and you will come to know what exception you are getting as from your mail it does looks clear what the problem is. So try this i hope this helps you.
abhinav
[This message has been edited by Abhinav Sharma (edited November 23, 2001).]
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!