File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes Overloded parseInt(String, int)? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Overloded parseInt(String, int)?" Watch "Overloded parseInt(String, int)?" New topic

Overloded parseInt(String, int)?

Ahmer Arman

Joined: Nov 23, 2001
Posts: 6
Thanks for giving reply of my question. Their is another method with the name of parseInt it has two arguments
parseInt(String, int);
I cannot understand the meaning of int argument in this method.
Please help me in this regard.
Again thanks to all who gave me reply.
Ahmer Arman

Ahmer Arman
Dave Vick
Ranch Hand

Joined: May 10, 2001
Posts: 3244
The int argument is the radix that the String integer is in. In other words its base. A String such as "12349" would be base 10 so the parseInt call would be parseInt("12349", 10); .
A String that is a hex representation could be something like "AF", so parseInt would be parseInt("AF", 16);.
Basicaly your telling the method that this String your passing it is in a different base than base 10.
hope that helps

Sun Certified Programmer for the Java� 2 Platform

Cindy Glass
"The Hood"

Joined: Sep 29, 2000
Posts: 8521
Well, since any number of classes COULD have a parseInt method - I am going to use my ESP and guess that you are talking about the one in the Integer class.
That method is just saying that if you feed in a number in String format, and you specify that "base" that the number is being displayed in (that would be the second parameter), the method will feed you back the value of that number in base 10.
Of course the String can only use digits that are valid in the base that you say that it is in - so if you say it is in base 5, then the String should only have digits 0,1,2,3 and 4 or else it will throw a NumberFormatException.
The api gives a whole list of examples:

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

"JavaRanch, where the deer and the Certified play" - David O'Meara
I agree. Here's the link:
subject: Overloded parseInt(String, int)?
It's not a secret anymore!