Help coderanch get a
new server
by contributing to the fundraiser
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

numberformatexception

 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can anybody help me how to solve the numberformatexception..

I have a form in which i have different types of field.One of them is to input the number in the textfield.
After i submit the form,I get that value using the following code in anothor jsp page.

String totalpages = request.getParameter("totalpage");
int totalpage = Integer.parseInt(totalpages);

But the problem is that,when i leave this textbox empty without entering any value and submit the form,i get the numberformat exception because of the empty string "".How to parse this one?
 
Sheriff
Posts: 67750
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You need to deal with the exception in any case. What if the user enters a non-numeric string?

What do you want to have happen in such cases?
 
Bartender
Posts: 4116
72
Mac TypeScript Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

How to parse this one?



You can validate the field (may using java script) not to have null and characters. Or before you parse it check for null value.
 
Ranch Hand
Posts: 151
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

You can validate the field (may using java script) not to have null and characters.

Better option would be to validate to a number using javascript.
 
sanat meher
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
When i enter the nonnumeric string also,it shows the numberformatexception,it means that it is unable to parse the string to number.

Validating the field with java script will do..but i need some alternate solution for the same in java.

Thanks
Sanat
 
rakesh sugirtharaj
Ranch Hand
Posts: 151
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I dont think using java for this is the best solution and i surely dont know why you are hesitating to use javascript when whole world is going ga-ga over web2.0 and RIA. Anyway, you can catch the exception and forward to an error page. Frameworks like Struts have specilaized options for this sort of validation.
 
sanat meher
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks everyone.
I think i have to validate this using javascript.I dont have any other option.

I dont know WEB 2 for now,but expecting to learn it.

Thanks
Sanat
 
Ranch Hand
Posts: 266
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by sanat meher:
thanks everyone.
I think i have to validate this using javascript.I dont have any other option.



You can put the parseInt part in a try catch block. An exception means invalid data and you can inform the user of the same. Generally it is not a good idea to rely only on JavaScript for data validation. It can be disabled in the browser. Even if it is enabled there are ways in which HTTP request can be tampered with.
 
Ranch Hand
Posts: 300
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I agree with Satish. You can handle this exception and can return the user on the same page.
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Before getting the valeus from request, You have to check whether the values is empty or not .
Hope the below code will resolve your Exception

String totalpages = "0";
if(request.getParameter("totalpage")!=null && !request.getParameter("totalpage").toString().trim().equals("")){
totalpages = request.getParameter("totalpage").toString().trim();
}
int totalpage = Integer.parseInt(totalpages);



Thanks and regars
Ajay Krishnamurthy
 
reply
    Bookmark Topic Watch Topic
  • New Topic