| Author |
Getting NumberFormat Exception
|
sandy mahajan
Greenhorn
Joined: Mar 27, 2008
Posts: 6
|
|
Hi, String str1=request.getParameter("str1"); if(str1==null) str1=""; using this code iam getting string(No Error). But i want to convert that string into int & i code int intstr1= Integer.parseInt(str1); & i got following exception java.lang.NumberFormatException: For input string: "" java.lang.NumberFormatException.forInputString(NumberFormatException.java:48) java.lang.Integer.parseInt(Integer.java:489) java.lang.Integer.parseInt(Integer.java:518) org.apache.jsp.jsp.Play_jsp._jspService(Play_jsp.java:109) org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:94) javax.servlet.http.HttpServlet.service(HttpServlet.java:802) org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:324) org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:292) org.apache.jasper.servlet.JspServlet.service(JspServlet.java:236) javax.servlet.http.HttpServlet.service(HttpServlet.java:802) org.apache.jsp.jsp.DummyController_jsp._jspService(DummyController_jsp.java:84) org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:94) javax.servlet.http.HttpServlet.service(HttpServlet.java:802) org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:324) org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:292) org.apache.jasper.servlet.JspServlet.service(JspServlet.java:236) javax.servlet.http.HttpServlet.service(HttpServlet.java:802) Please show me the right way.....Thanks.
|
 |
K.Suresh Kumar
Ranch Hand
Joined: Nov 21, 2007
Posts: 41
|
|
Hi , you try convert null value to integer value. So that you are getting NumberFormatException. Your code : yon can't convert null value to int value. Regards, Suresh Kumar.K
|
 |
Muhammad Saifuddin
Ranch Hand
Joined: Dec 06, 2005
Posts: 1318
|
|
doesn't make sense, when you initialized st1 to "" after getting the value through request object.
|
Saifuddin..
[Linkedin] How To Ask Questions On JavaRanch My OpenSource
|
 |
sandy mahajan
Greenhorn
Joined: Mar 27, 2008
Posts: 6
|
|
Hi, I generated Random no. & retrives througth this code String str1=request.getParameter("str1"); // getting String ex."34" if(str1==null) str1=""; but i want to convert to int for comparision & code int intstr1= Integer.parseInt(str1); but got NumberFormat exception As per reply to me & i code String str1=request.getParameter("str1"); // getting String ex."34" if(str1==null) str1="0"; int intstr1= Integer.parseInt(str1); but same error occurs with str1="0" append with String java.lang.NumberFormatException: For input string: "071 " java.lang.NumberFormatException.forInputString(NumberFormatException.java:48) java.lang.Integer.parseInt(Integer.java:477) java.lang.Integer.parseInt(Integer.java:518) org.apache.jsp.jsp.Play_jsp._jspService(Play_jsp.java:109) org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:94) javax.servlet.http.HttpServlet.service(HttpServlet.java:802) org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:324) org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:292) org.apache.jasper.servlet.JspServlet.service(JspServlet.java:236) javax.servlet.http.HttpServlet.service(HttpServlet.java:802) org.apache.jsp.jsp.DummyController_jsp._jspService(DummyController_jsp.java:84) org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:94) javax.servlet.http.HttpServlet.service(HttpServlet.java:802) org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:324) org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:292) org.apache.jasper.servlet.JspServlet.service(JspServlet.java:236) javax.servlet.http.HttpServlet.service(HttpServlet.java:802) I stuck at this point. Please show me the way......... Thanks
|
 |
Herman Schelti
Ranch Hand
Joined: Jul 17, 2006
Posts: 387
|
|
There's a space behind the 071 Herman
|
 |
Anubhav Anand
Ranch Hand
Joined: May 18, 2007
Posts: 341
|
|
Quick Tip: Please use code tags while posting code it makes a post much more readable and saves time. You may also like to see how to ask questions on JavaRanch.
|
 |
Jhakda Velu
Ranch Hand
Joined: Feb 26, 2008
Posts: 158
|
|
Hi You can use trim() method to avoid extra spaces. But ensure that you have done the null check else a null pointer ex will be thrown. Jhakda
|
If I become filthy rich, I'll sponsor research for painless dental treatment at Harvard Medical School. Thats why,I'm learning Java.I have 32 teeth, 22 are man made.
|
 |
 |
|
|
subject: Getting NumberFormat Exception
|
|
|