Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

numberformatexception

 
sanat meher
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?
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65111
89
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?
 
Vijitha Kumara
Bartender
Posts: 3914
9
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.
 
rakesh sugirtharaj
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
 
Satish Chilukuri
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.
 
Patricia Samuel
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.
 
ajju Krishnamurthy
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic