File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

NumberFormatException

 
Harold Ndou
Ranch Hand
Posts: 34
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, can anyone explain to me why Integer.parseInt can throw a NumberFormat Exception from the following code

try { int x = Integer.parseInt("two"); }

I will appriciate your assistance.
 
Nam Ha Minh
Ranch Hand
Posts: 502
Eclipse IDE Firefox Browser Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Simply "two" is a word, not a number.
 
James X Peterson
Whizlabs Java Support
Ranch Hand
Posts: 158
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Harold Ndou wrote:

try { int x = Integer.parseInt("two"); }


Integer.parseInt() method will take string as argument but that string need to be numericals not letters. If you pass numericals then it will convert to integer.
 
Mauro Mazzucco
Ranch Hand
Posts: 32
Eclipse IDE Java MySQL Database
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
parseInt doesnt know what "two" is, only "2"
 
Harold Ndou
Ranch Hand
Posts: 34
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks so much guys
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic