Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!eternal-september.org!feeder.eternal-september.org!.POSTED!not-for-mail From: Eric Sosman Newsgroups: comp.lang.java.help Subject: Re: Convert a String to Int: can I use toInt() instead of Integer.parseInt()? Date: Tue, 05 Jul 2011 16:27:04 -0400 Organization: A noiseless patient Spider Lines: 55 Message-ID: References: <521s0711i3d7bsc8620isi0oiidl91kujq@4ax.com> <7an6179km7upsvba40ebe98gmfedg2414k@4ax.com> Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Injection-Date: Tue, 5 Jul 2011 20:28:28 +0000 (UTC) Injection-Info: mx04.eternal-september.org; posting-host="BrOwaJANne849xlH+KPYjQ"; logging-data="30070"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18z5yPKg5Rqh6EFnz738Kx9" User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2.18) Gecko/20110616 Thunderbird/3.1.11 In-Reply-To: <7an6179km7upsvba40ebe98gmfedg2414k@4ax.com> Cancel-Lock: sha1:5L2KL5rfvSlHm5HH3ePKPTmJNiA= Xref: x330-a1.tempe.blueboxinc.net comp.lang.java.help:821 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