Re: Convert a String to Int: can I use toInt() instead of Integer.parseInt()?
On 7/5/2011 2:57 PM, Thee Chicago Wolf [MVP] wrote:
On 7/5/2011 11:26 AM, Thee Chicago Wolf [MVP] wrote:
[...] regarding the toInt() method, have a look here:
http://www.iitk.ac.in/esc101/04Jul/javabookdoc/javabook/Convert.html
Fine: Not a part of Java, but something from a third-party
"javabook" package. And, since it returns an `int', it doesn't
suit your requirement to convert thousand-digit strings (unless,
as previously remarked, nearly all of those digits are zero).
I simply thought that since there's a nice toString() method, SURELY
in their wisdom the authors of the Java language would make its
compliment as simple as well.
"ComplEment." And it should be clear to you that the process
of converting a number to a string representation is quite different
from that of converting an arbitrary string to a number. What, for
example, is the numeric equivalent of "1234%6789"?
The numeric equivalent of "1234%6789" is PURPLE.
Fine. Do you now see why the string-to-number problem is an
entirely different from the number-to-string problem? And much
more intricate, too?
In the instance where it is a long string of numbers (no, not a whole
bunch of zeros) and no symbols are there, it should be able to convert
from String to an integer value just fine. Because that situation
never happens in real life right? The limit is because you can't
convert a string of numbers to int past 9 digits or it bombs out.
... and it depends on which nine digits, too: "111111111" is
just fine as an `int', while "555555555" is not. I imagine that
your homework exercise is intended to bring you to an understanding
of this limitation, and also intended to introduce you to other
ways of representing integers than as `int' values.
It would have been nice to have a nifty method to do this back and
forth conversion.
Two methods, surely. And I hope by now you understand that
integer-to-string is a far simpler matter than string-to-integer.
(Floating-point-to-string is harder, but still simpler than
string-to-floating-point.)
Realistically, everything is a string until you try
and do a math op on it, right?
I'm afraid I'm not equal to the intellectual pressure of
the conversation.
--
Eric Sosman
esosman@ieee-dot-org.invalid