• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Jeanne Boyarsky
  • Ron McLeod
  • Tim Cooke
Sheriffs:
  • Devaka Cooray
  • paul wheaton
  • Mark Herschberg
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Frits Walraven
  • Jj Roberts
Bartenders:
  • Carey Brown
  • salvin francis
  • Piet Souris

exception while converting string to int

 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello friends,
I am trying to convert string to Int but i am getting exception as java.lang.NumberFormatException: For input string: ""
Code is as follows

String no=request.getParameter(itemno); // here itemno varies everytime...sometime it will be 1 and sometime it will be 2 but in string format
String itemn[]=no.split(" ");
int itemNo=Integer.parseInt(itemn[0]);

Can anyone solve this problem for me??

Thanks in advance
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The error message is telling you exactly what's wrong. The empty string ("") is not a valid representation of any int. If you print out your "no" variable and each element of your "itemn" array, you'll see what's going on at that point, and then you can work backwards to figure out how that ended up being wrong.
 
Greenhorn
Posts: 23
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Bharat Sankannanavar wrote:Hello friends,
I am trying to convert string to Int but i am getting exception as java.lang.NumberFormatException: For input string: ""
Code is as follows

String no=request.getParameter(itemno); // here itemno varies everytime...sometime it will be 1 and sometime it will be 2 but in string format
String itemn[]=no.split(" ");
int itemNo=Integer.parseInt(itemn[0]);

Can anyone solve this problem for me??

Thanks in advance




Are You working with Servlet/JSP

and if you are then there seems no problem in the above code
use a try/catch block for the above code with a NumberFormatException object
if still not clear you can again quote on it
 
Bharat Sankannanavar
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the quick reply Jeff,
One small correction is there... Its giving exception as java.lang.NumberFormatException: For input string: "1" when the itemno is 1 and exception as java.lang.NumberFormatException: For input string: "2" when the itemno is 2.
 
Bharat Sankannanavar
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Digen Mahara, I am working on JSP.

When I used NumberFormatException its giving as itemNo cannot be resolved to a variable
 
lowercase baba
Posts: 12963
66
Chrome Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
a good debugging tip would be to print out the string just before you try and convert it, to make sure it holds what you really think it holds. I'll usually put something both before and after it, to make sure I can see trailing white-space characters...something like

You'll be amazed at how often the string isn't what you think it is...
 
Bartender
Posts: 10777
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

fred rosenberger wrote:You'll be amazed at how often the string isn't what you think it is...


And yet we have more and more (mostly Web) paradigms that are based on passing Strings around.

@Bharat: I've found this little utility method (pre v7 you understand) very useful:and I'm sure you could extend it to include an "integer", viz:
Winston
 
Marshal
Posts: 72048
312
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That looks a nice way to avoid the Exception, but maybe sometimes the Exception is the correct thing to do.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:That looks a nice way to avoid the Exception, but maybe sometimes the Exception is the correct thing to do.



++

In this case, if it's an empty string, then 0 may be a perfectly valid equivalent, such as for quantities in an online order form with multiple products listed in one form.

However, the OP has indicated that he has (or thinks he has) a "1", not an empty String. If that's the case, then you definitely want the exception.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

fred rosenberger wrote:a good debugging tip would be to print out the string just before you try and convert it, to make sure it holds what you really think it holds. I'll usually put something both before and after it, to make sure I can see trailing white-space characters...something like

You'll be amazed at how often the string isn't what you think it is...



Indeed. And sometimes we even have to go a couple steps further. Such as...

Print the string's length(), in case there are additional invisible characters that are screwing up the parsing.

For each character in the string, print out its codepoint value by casting it to an int, in case it's a char from a different charset that looks like a valid digit but isn't, or in case it's something like a lowercase "el" instead of a one or an uppercase "OH" instead of a zero.
 
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Bharat, please BeForthrightWhenCrossPostingToOtherSites
http://www.java-forums.org/exception-codes/53700-exception-while-converting-string-integer.html
 
Winston Gutkowski
Bartender
Posts: 10777
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jeff Verdegan wrote:Print the string's length(), in case there are additional invisible characters that are screwing up the parsing...


Doh! You and I (and anybody who's ever read Josh) knows that Strings are NOT substitutes for other objects.
Aren't; never have been; never will be. The problem is that they can look like another object - particularly when it comes to representing its value.

Anybody but me see the danger of "String"-based communication?

Winston
 
Ranch Hand
Posts: 808
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Winston Gutkowski wrote:

Jeff Verdegan wrote:Print the string's length(), in case there are additional invisible characters that are screwing up the parsing...


Doh! You and I (and anybody who's ever read Josh) knows that Strings are NOT substitutes for other objects.
Aren't; never have been; never will be. The problem is that they can look like another object - particularly when it comes to representing its value.

Anybody but me see the danger of "String"-based communication?

Winston


I am with you. I was largely unable to persuade my project leads of this, and so we now have the unhappy chore of validating the formats of all our Strings to make sure that they really represent the entities they claim to.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Winston Gutkowski wrote:

Jeff Verdegan wrote:Print the string's length(), in case there are additional invisible characters that are screwing up the parsing...


Doh! You and I (and anybody who's ever read Josh) knows that Strings are NOT substitutes for other objects.
Aren't; never have been; never will be. The problem is that they can look like another object - particularly when it comes to representing its value.

Anybody but me see the danger of "String"-based communication?

Winston



You mean like storing numbers as varchar in a DB? https://coderanch.com/t/564021/java/java/Long-value-java
 
Winston Gutkowski
Bartender
Posts: 10777
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jeff Verdegan wrote:You mean like storing numbers as varchar in a DB? https://coderanch.com/t/564021/java/java/Long-value-java


Precisely. And I totally agree with your comment there: Ick.

Winston
 
Bharat Sankannanavar
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
fred rosenberger, I already tried printing the string before converting it into Integer. Its containing the valid string which can be converted into Integer but prefixed with a space. So here I am trying to remove that space but couldn't able to do that.
 
Rancher
Posts: 3742
16
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Bharat Sankannanavar wrote:I am trying to remove that space but couldn't able to do that.


Have you looked at the String javadoc to see if there is a method that will do that for you ?
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Joanne Neal wrote:

Bharat Sankannanavar wrote:I am trying to remove that space but couldn't able to do that.


Have you looked at the String javadoc to see if there is a method that will do that for you ?



And what exactly do you mean by "tried" and "couldn't"? It's rather difficult to tell you where you went wrong if you don't show exactly what you tried and exactly what results you got.
 
The two armies met. But instead of battle, they decided to eat some pie and contemplate this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic