Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Integer.parseInt : NumberFormatException

 
Clive van Hilten
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please would someone tell me why the following Integer.parseInt statement throws a NumberFormatException:
String string = properties.getProperty("flag");
int i = Integer.parseInt(string);
The value of string is "0" ("zero"). The error also occurs if string is "1".
Thanks.
[ March 12, 2003: Message edited by: Clive van Hilten ]
 
Chris Shepherd
Ranch Hand
Posts: 286
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hmm only things I can think of is to check the length and make sure you don't have any extra spaces in there. ALso, try printing it out to your console right before you try to parse it to be sure it really is the value you expect.
My guess is that your string isn't what you think it is. You could test the parsing function by putting a known string value("0" or "1") directly into the parse method to prove that it works. It will, I'm sure, but just to test all points of failure...
HTH
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This works as expected:
 
William Barnes
Ranch Hand
Posts: 986
Eclipse IDE Firefox Browser Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Catch the exception and print it out. That will give a clue as to what is happening.
 
William Barnes
Ranch Hand
Posts: 986
Eclipse IDE Firefox Browser Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
jantar mantar
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
as chris mentioned earlier, most of the time, the problem is with the blank spaces.
Try using :
.
The trim function of the string class will chop of any spaces in ur string.
Hope this helps.
'amit
 
Clive van Hilten
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks very much for all the replies - the problem turned out to be a blank space - the trim() method sorted it out!
Clive
[ March 13, 2003: Message edited by: Clive van Hilten ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic