parseInt...

public class TestParseInt  {
    public static void main(String[ ] args) {
//        int n=Integer.parseInt("12");     //A  no error because 12 is string
//        int n=Integer.parseInt("12", 2);  //B  has error because ?      
//        int n=Integer.parseInt("345", 8); //C  no error because ?
//        int n=Integer.parseInt("100");    //D  no error because 100 is string
//        int n=Integer.parseInt(100);      //E  has error because 100 is not string
//        int n=Integer.parseInt(100, 16);  //F  has error because 100 is not string
    }  
}

in the front of each line, I have written my understanding of why there is or is not a compile error.

Please comment and enhance these statements.

Thank you.
LVL 34
Mike EghtebasDatabase and Application DeveloperAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

for_yanCommented:
B has errior and second paramemetr is radix - if your radix is 2 - you cannot have digit 2 in binary number
0
for_yanCommented:
C has no error as 3,4,5 - a vialid digits in radix 8
0
for_yanCommented:
Threst of thinsg are obvious - you cannot have int when you need String and vice versa - ther are just no such methiods with parseInt(int, int)
or parseInt(int)
0
for_yanCommented:
There are jsut two methods parseInt in Integer class:

http://docs.oracle.com/javase/6/docs/api/java/lang/Integer.html

----------------------
static int       parseInt(String s)
          Parses the string argument as a signed decimal integer.
static int       parseInt(String s, int radix)
          Parses the string argument as a signed integer in the radix specified by the second argument.
----------------------

Both have first argument of type String
So any parseInt which have forst argument not a String will fail

The second argument is optional - it is int and has a meaning of radix
So obviously the string should conform to the radix, so strings continaing digit 2 with radix 2
cannot be interpreed as int - so paarseInt ("12",2) - is not legal, as binary numbers should have only 0 and 1
"345",8 on the contrary is fine, as any digits less than 8will be fine with octal numbers
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.