wood burning stoves 2.0
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Extracting INT value from  a  STRING Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Extracting INT value from  a  STRING" Watch "Extracting INT value from  a  STRING" New topic
Author

Extracting INT value from a STRING

Brett Swift
Ranch Hand

Joined: May 22, 2002
Posts: 61
I'm trying to figure out how to extract an int value from the command line. It came up in my head as something you'd think to be fundamental, but I can't figure it out!?!
I've tried parseInt(args[0]); valueOf(args[0]); but maybe I've got the syntax wrong. (not too familiar with those methods.. probably using them wrong!) Anyone have any tips? Thanks!
Brett
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
pareInt is a static method of Integer, so just use this:

That should do the trick. Of course, you'll probably want to catch a NumberFormatException (which is a runtime exception) in the case that the first argument isn't a valid int.
Corey


SCJP Tipline, etc.
Brett Swift
Ranch Hand

Joined: May 22, 2002
Posts: 61
thanks.. I now remember having this problem before and all it was ....was forgeting to reference the Integer class in front of the method call!
Thanks!
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Extracting INT value from a STRING
 
It's not a secret anymore!