Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!news.albasani.net!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail From: Nigel Wade 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 09:57:02 +0100 Lines: 72 Message-ID: <97imauF3tbU1@mid.individual.net> References: <521s0711i3d7bsc8620isi0oiidl91kujq@4ax.com> <7an6179km7upsvba40ebe98gmfedg2414k@4ax.com> Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit X-Trace: individual.net sRtS7t+y+Tu5kZLb8TuWyQKW/axrYzRkBajgqEbA2xJOgF8EQW Cancel-Lock: sha1:ENMtazeIELAaT9z8c47McOkm+Iw= User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-GB; rv:1.9.1.16) Gecko/20101125 SUSE/3.0.11 Thunderbird/3.0.11 In-Reply-To: Xref: x330-a1.tempe.blueboxinc.net comp.lang.java.help:824 On 05/07/11 21:47, Thee Chicago Wolf [MVP] wrote: >> On 7/5/2011 11:57 AM, Thee Chicago Wolf [MVP] wrote: >>> Realistically, everything is a string until you try >>> and do a math op on it, right? >> >> >> No, there are very large differences between the internal representation >> of an integer and the internal representation of a String. >> >> At least in Java, you can't do math on Strings. >> >> I'm curious now about your programming experience. It matters, at least >> in part, if you really need help. The part of your post I quoted above >> is very strange to me, even nonsensical, so I'm asking why you think >> strings and numbers are somehow equivalent, because they aren't. >> >> Also, what exactly is your assignment? Are you allowed to use >> BigInteger? Have you clarified with your instructor yet? > > Sorry if that was confusing. I meant to say that to Java, everything > is string input until it's assigned a data type. That's what my prof > says. Oh, dear. He sounds like a Python programmer... It is not true of Java. > I don't have any programming experience, this is Programming II. > And yes, I know you cannot do math on String unless the String is > totally numeric in which case it first must be converted to integer > and then have a math op done on it. So when I stated that everything > is a string until you do a math op on it, I did not mean literally > that. I understand that data type manipulation has to happen first. > > The assignment, exactly, was: > > 1. You have a 1000-digit string of numbers. > 2. Go through that string of numbers and find the 5 consecutive > numbers whose product is the largest and print that product. > 3. Print the 5 consecutive numbers that produced the largest product. > > So basically it was a matter of 1) loading up an array with the 1000 > digits by "converting" the String to int using Integer.parseInt() and > 2) using two for loops--one to load the array and one to do > multiplication using x[i]*x[i+1]*x[i+2]*x[i+3]*x[i+4] as an algorithm. > i would obviously increment to go through the array to the end minus 5 > (to prevent out-of-bounds. i.e., x.length-5). That worked for me. That's rather a verbose way of getting things done. You might want to look at the Javadocs for the String class. Look for methods which return a substring (in particular one which meets your requirement to extract any 5 consecutive characters from your 1000 character string). Then look at the methods of the Integer (or Long) class to find one which can parse that substring into a number. A simple loop around that, and a check for a max., ought to get the job done. Javadocs are your best friend when learning and/or programming in Java, and the Java Tutorials will help you with simple worked examples. I still refer to them on a regular basis. Don't catch IDE'itis (where you choose a method based on what pops up in the IDE completion list). Until you know the methods in detail, look them up in the Javadocs. > > The assignment did not say we *couldn't* use BigInteger. How would > that have helped? Haven't used it yet. Does it say you have to load the string into an int array? If you don't, then I wouldn't. -- Nigel Wade