• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • Devaka Cooray
Saloon Keepers:
  • Ganesh Patekar
  • Tim Moores
  • Carey Brown
  • Stephan van Hulst
  • salvin francis
Bartenders:
  • Ron McLeod
  • Frits Walraven
  • Pete Letkeman

numberformatexception  RSS feed

 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • 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?
 
Author and ninkuma
Marshal
Posts: 66823
168
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • 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: 4053
54
Chrome Fedora Hibernate
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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
  • 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
  • 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
  • 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
  • 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
  • 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
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!