• 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

Integer.parseInt : NumberFormatException

 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Ranch Hand
Posts: 286
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This works as expected:
 
Ranch Hand
Posts: 1067
2
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 1067
2
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
reply
    Bookmark Topic Watch Topic
  • New Topic