wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Getting NumberFormat Exception Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
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: 1321



doesn't make sense, when you initialized st1 to "" after getting the value through request object.


Saifuddin..
[Blog][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: 166
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.
danjonila Jkatz
Greenhorn

Joined: Aug 12, 2013
Posts: 1
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

Joined: Sep 13, 2007
Posts: 2234
    
    7

@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.


K. Tsang JavaRanch SCJP5 SCJD/OCM-JD OCPJP7 OCPWCD5
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14074
    
  16

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.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
 
 
subject: Getting NumberFormat Exception
 
Similar Threads
Journal Article - JavaServer Faces 1.0 Part I
java.lang.NullPointerException in Opening JSP fle
Error while jsp connecting to database
getting exception in JSF Tutorial of JR
Unable to convert lang.Object to int [solved]