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: markspace <-@.> Newsgroups: comp.lang.java.help Subject: Re: Convert a String to Int: can I use toInt() instead of Integer.parseInt()? Date: Wed, 06 Jul 2011 15:36:56 -0700 Organization: A noiseless patient Spider Lines: 30 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: Wed, 6 Jul 2011 22:37:01 +0000 (UTC) Injection-Info: mx04.eternal-september.org; posting-host="5HSAJfqnDjjLFxXZ6WBWEw"; logging-data="21886"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/nSSQ76X7En88kD8uN1uxJ2SSKfawqIOA=" User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.2.18) Gecko/20110616 Thunderbird/3.1.11 In-Reply-To: Cancel-Lock: sha1:qZnhhYc7tiz9cNMYN7R+sAh4dBA= Xref: x330-a1.tempe.blueboxinc.net comp.lang.java.help:833 On 7/6/2011 2:58 PM, Thee Chicago Wolf [MVP] wrote: > Well, I originally used int[] x = new int[str.length]; but it gives a > compile error. (str.length()] does work though. Guessing because it's Yup, I missed that one, it's length(). > stuff that I used. Thanks for that tip! But does the - '0' portion of > x[i] = str.charAt(i) - '0'; just tell it to convert to its ASCII > value? We dicussed this here and were like WTF is this? What exactly > is that portion doing? Thanks for walking me through it. For ASCII values, UTF (what Java uses) and ASCII are the same values. "char" is an integer already, there's no need to convert. str.charAt( x ) already returns a number, just a number encoded to mean a UTF character (code point?). So, you just have to map one number range to another. '0' is 48 decimal. E.g.: '0' - '0' = 48 - 48 = 0. '1' - '0' = 49 - 48 = 1. '2' - '0' = 50 - 48 = 2. etc. Remember Neo in The Matrix? It's all just number, really, even the letters.