It's not a secret anymore!
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes parseInt & parseLong Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "parseInt & parseLong" Watch "parseInt & parseLong" New topic

parseInt & parseLong

Calvin Yan

Joined: Jun 25, 2003
Posts: 19
When I reading an article on wrapper class , I found two interesting question and hope someone could give me the explanation:
parseInt("Kona",27) return 411787
parseLong("Hazelunt",36) return 1356099454469L

I will do the best with what God gave me!
Anupam Sinha
Ranch Hand

Joined: Apr 13, 2003
Posts: 1090
Hi Calvin
This will help you. You can read more about the Integer class here.
public static int parseInt(String s,
int radix)
throws NumberFormatException
Parses the string argument as a signed integer in the radix specified by the second argument. The characters in the string must all be digits of the specified radix (as determined by whether Character.digit(char, int) returns a nonnegative value), except that the first character may be an ASCII minus sign '-' ('\u002D') to indicate a negative value. The resulting integer value is returned.
An exception of type NumberFormatException is thrown if any of the following situations occurs:
The first argument is null or is a string of length zero.
The radix is either smaller than Character.MIN_RADIX or larger than Character.MAX_RADIX.
Any character of the string is not a digit of the specified radix, except that the first character may be a minus sign '-' ('\u002D') provided that the string is longer than length 1.
The value represented by the string is not a value of type int.
parseInt("0", 10) returns 0
parseInt("473", 10) returns 473
parseInt("-0", 10) returns 0
parseInt("-FF", 16) returns -255
parseInt("1100110", 2) returns 102
parseInt("2147483647", 10) returns 2147483647
parseInt("-2147483648", 10) returns -2147483648
parseInt("2147483648", 10) throws a NumberFormatException
parseInt("99", 8) throws a NumberFormatException
parseInt("Kona", 10) throws a NumberFormatException
parseInt("Kona", 27) returns 411787

s - the String containing the integer representation to be parsed
radix - the radix to be used while parsing s.
the integer represented by the string argument in the specified radix.
NumberFormatException - if the String does not contain a parsable int.
Valentin Crettaz
Gold Digger

Joined: Aug 26, 2001
Posts: 7610
Welcome to Javaranch, a friendly place for Java greenhorns
We ain't got many rules 'round these parts, but we do got one. Please change your displayed name to comply with the JavaRanch Naming Policy.
Thanks Pardner! Hope to see you 'round the Ranch!

[Blog] [Blogroll] [My Reviews] My Linked In
I agree. Here's the link:
subject: parseInt & parseLong
It's not a secret anymore!