• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Getting NumberFormat Exception

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 1325
Android Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 387
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


There's a space behind the 071

Herman
 
Ranch Hand
Posts: 341
Firefox Browser Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 167
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Bartender
Posts: 3648
16
Android Mac OS X Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic