aspose file tools
The moose likes Beginning Java and the fly likes Getting NumberFormat Exception Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Beginning Java
Reply Bookmark "Getting NumberFormat Exception" Watch "Getting NumberFormat Exception" New topic
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.
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: Getting NumberFormat Exception
 
Similar Threads
Unable to convert lang.Object to int [solved]
Journal Article - JavaServer Faces 1.0 Part I
Error while jsp connecting to database
getting exception in JSF Tutorial of JR
java.lang.NullPointerException in Opening JSP fle