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

Getting NumberFormat Exception

 
sandy mahajan
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1324
Android Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


doesn't make sense, when you initialized st1 to "" after getting the value through request object.
 
sandy mahajan
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 387
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


There's a space behind the 071

Herman
 
Anubhav Anand
Ranch Hand
Posts: 341
Firefox Browser Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 167
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
danjonila Jkatz
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have another question regarding my code using Java 1.4.2.

Here is my source code:





And here is the error I get:


Exception in thread "main" java.lang.NumberFormatException: For input string: ""
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:48)
at java.lang.Integer.parseInt(Integer.java:470)
at java.lang.Integer.valueOf(Integer.java:554)
at WriteKong.sort(WriteKong.java:58)
at WriteKong.main(WriteKong.java:25)
Macintosh-d49a20e0a718:WriteKong arnykatz$ javac WriteKong.java
Macintosh-d49a20e0a718:WriteKong arnykatz$ java WriteKong
StudentID = 7493272
Course = English
MarkS =
Exception in thread "main" java.lang.NumberFormatException: For input string: ""
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:48)
at java.lang.Integer.parseInt(Integer.java:470)
at java.lang.Integer.valueOf(Integer.java:554)
at WriteKong.sort(WriteKong.java:58)
at WriteKong.main(WriteKong.java:25)


Why do I get this error?
 
K. Tsang
Bartender
Posts: 3502
14
Android Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@danjonila Your error is the same as the OP: passing in a "" (blank) to the Integer.valueOf() on line 58. Just like what the OP did, you need to check for null.
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Pie
Posts: 15437
41
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to JavaRanch, Danjonila.

Is there a special reason why you are using Java 1.4.2? That is a very old and outdated version of Java. I advise you to use Java 7, the current version. You can download the JDK for Java 7 on Oracle's website.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic