This week's book giveaway is in the OCPJP forum. We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line! See this thread for details.
does anyone know how to convert String to primitive int without use of Wrapper classes.
Not sure I understand what you are asking. Do you want to do something like
without using the nice builtin parse functions?
The obvious way is to convert the String into a byte array, then iterate over the array. Pick up a byte, subtract "0" (ascii character zero) to get the value as an integer, then add it, and multiply by ten every time you pick up the next character.
Not sure this is really "advanced" so maybe I'm not understanding.
Joined: Oct 11, 2006
I shall make my question clear to you.. For example String s="12"; I want to know how this string can be converted to primitive int without using the methods of Wrapper Class ie Integer class methods should not be used to convert it to primitive. Thanks in advance..
Following program shows a logic to convert a string to int without using wrapper class. But this logic will work when input is only a positive number. For negative number input you can modify the logic.
[ August 21, 2007: Message edited by: prakash chandra ] [ August 24, 2007: Message edited by: prakash chandra ]
Anil, I think it would help us to help you if you told us *why* you are interested in this.
The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus