Path: csiph.com!x330-a1.tempe.blueboxinc.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!news.glorb.com!news-out.readnews.com!news-xxxfer.readnews.com!s09-10.readnews.com!posts.news.usenetmonster.com!nnrp3-unl.asbnva.usenetmonster.com!not-for-mail From: "Thee Chicago Wolf [MVP]" <.@.> 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 13:11:54 -0500 Organization: . Reply-To: . Message-ID: References: <521s0711i3d7bsc8620isi0oiidl91kujq@4ax.com> <7an6179km7upsvba40ebe98gmfedg2414k@4ax.com> X-Newsreader: Forte Agent 6.00/32.1186 X-No-Archive: yes MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit Lines: 87 NNTP-Posting-Host: 3abf70fc.unlimited.usenetmonster.com X-Trace: DXC=OEoIBKf9iSTOn 7/5/2011 1:47 PM, Thee Chicago Wolf [MVP] wrote: > >> >> 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. > > >Your prof is mistaken. It is completely possible to read binary images >of number directly off the disk or wire without using string >conversions. I know he's simplifying, but it's the kind of >simplification that causes confusion and misunderstanding later. Plus >he's wrong. > > >> I don't have any programming experience, this is Programming II. > > >Programming I is "experience," it's fine to say where you're at now. > > >> 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. > > >Oh, that's a bit different than what you stated. I think we all assumed >that your question was how to read a single 1000 digit integer (which I >think is what you actually said). > > >> The assignment did not say we *couldn't* use BigInteger. How would >> that have helped? Haven't used it yet. > > >For example: > > BigInteger bi = > new BigInteger( "9872345804321275419287439182374123489894378" ); > >where that String is too large to be represented as a number by an int >(or long, I think). Of course, it's entirely possible I misunderstood my prof when he said it was all string input. Nevermind, I'm still learning and maybe just don't yet grasp what he's talking about. In my case, just short of 39 lines of code did what I needed. One damnable problem that baffled me was my first for loop: for (int i = 0; i < str.length(); i++) { String index = str.charAt(i)+""; x[i] = Integer.parseInt(index); } I remember in programming I using charAt() for some homework but it didn't require me to do +"". A friend said I had to do that (meaning adding +"" after str.charAt(i) to make it work. It was because I was reading from a String type instead of a char type. Below is a shortened example of both the String and array I set up. Would there have been a better way for me to load the array using chatAt() (or some similar method) without having to add +"" to make it do what I wanted? Adding +"" seemed to be a bandage--albeit a working one--- but just for my own understanding, I want to know the proverbial "better" way to do what I was intending. Is there something else I could have used instead? String str = "30420729485092847590"; int[] x = new int[20]; for (int i = 0; i < str.length(); i++) { String index = str.charAt(i)+""; x[i] = Integer.parseInt(index); } I'd be willing to post the whole thing but I'd probably have to change the string so as not to piss of my prof should he do a Google search on the string he provided. Cheers. - Thee Chicago Wolf [MVP]