aspose file tools*
The moose likes Beginning Java and the fly likes numberformatexception Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "numberformatexception" Watch "numberformatexception" New topic
Author

numberformatexception

sanat meher
Greenhorn

Joined: Nov 06, 2008
Posts: 18
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

Joined: Jan 10, 2002
Posts: 60057
    
  65

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?


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Vijitha Kumara
Bartender

Joined: Mar 24, 2008
Posts: 3775

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.


SCJP 5 | SCWCD 5
[How to ask questions] [Twitter]
rakesh sugirtharaj
Ranch Hand

Joined: Dec 16, 2007
Posts: 151
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.


Cheers!
RSR
sanat meher
Greenhorn

Joined: Nov 06, 2008
Posts: 18
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

Joined: Dec 16, 2007
Posts: 151
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

Joined: Nov 06, 2008
Posts: 18
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

Joined: Jun 23, 2005
Posts: 266
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

Joined: Sep 12, 2007
Posts: 300
I agree with Satish. You can handle this exception and can return the user on the same page.
ajju Krishnamurthy
Greenhorn

Joined: Nov 14, 2008
Posts: 4
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
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: numberformatexception
 
Similar Threads
JSF immediate=true: Bug?
JTable Cell data validation/blank cell problem
Difference between getAttribute(string) and getParameter(String)
How to get an integer value from controller to jsp
Paging problem in jsp