wood burning stoves 2.0*
The moose likes Mock Exam Errata and the fly likes parseInt() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Mock Exam Errata
Bookmark "parseInt()" Watch "parseInt()" New topic
Author

parseInt()

sriram gupta
Ranch Hand

Joined: Aug 09, 2001
Posts: 39
I compiled the following code and the output is as follows?
Can someone explain why is it so?
__________________________________________________________

public class BinDec{
public static void main(String argv[]){
System.out.println(Integer.parseInt("011",3));
System.out.println(Integer.toString(64,2));
}
}
__________________________________________________________

If you compile and run this program the output will be
4
1000000
CAN SOMEONE EXPLAIN WHY?? if anybody can tell me how do we use parseInt() it will be very helpful...
thanks in advance...
Cherry Sta. Romana
Greenhorn

Joined: Aug 08, 2001
Posts: 18
Hi,

In the first statement
System.out.println(Integer.parseInt("011",3));
The output is 4 because you specified a radix/base of 3,
011 = 1*3 + 1 = 4
In the second case
System.out.println(Integer.toString(64,2));
The output is 1000000 because you specified base 2 and 64 in binary is 1000000.
If you want to simply convert to integer, do not put any radix.
I hope this helps.


sriram gupta
Ranch Hand

Joined: Aug 09, 2001
Posts: 39
hi cherry,
thanks for ur help.. cna u tell me where i can read more about this.... i have R&H but i didn't find anything there and i am appearing for paper on this monday...
bye
Jane Griscti
Ranch Hand

Joined: Aug 30, 2000
Posts: 3141
Hi sriram,
The best place to get information on the Math and wrappper classes is straight from the API.
------------------
Jane Griscti
Sun Certified Programmer for the Java� 2 Platform
[This message has been edited by Jane Griscti (edited August 14, 2001).]


Jane Griscti
SCJP, Co-author Mike Meyers' Java 2 Certification Passport
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: parseInt()
 
Similar Threads
doubt with StringBuffer
Exception
int to string conversion using toStirng()
strings - marcus green mocks
Unusual output