Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.help > #828

Re: Convert a String to Int: can I use toInt() instead of Integer.parseInt()?

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 2011-07-06 13:11 -0500
Organization .
Message-ID <vf891713vu8quck6170207ec1bv17700cf@4ax.com> (permalink)
References (3 earlier) <iuvi8e$m6g$1@dont-email.me> <7an6179km7upsvba40ebe98gmfedg2414k@4ax.com> <iuvqj8$ilj$1@dont-email.me> <dss6179oa9vq21vf7tlu6e78bdhqjdm65m@4ax.com> <iv01d2$2g5$1@dont-email.me>

Show all headers | View raw


>On 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]

Back to comp.lang.java.help | Previous | NextPrevious in thread | Next in thread | Find similar


Thread

Convert a String to Int: can I use toInt() instead of Integer.parseInt()? "Thee Chicago Wolf [MVP]" <.@.> - 2011-07-01 12:53 -0500
  Re: Convert a String to Int: can I use toInt() instead of Integer.parseInt()? Jeff Higgins <jeff@invalid.invalid> - 2011-07-01 17:55 -0400
  Re: Convert a String to Int: can I use toInt() instead of Integer.parseInt()? markspace <-@.> - 2011-07-01 18:19 -0700
    Re: Convert a String to Int: can I use toInt() instead of Integer.parseInt()? "Thee Chicago Wolf [MVP]" <.@.> - 2011-07-05 10:26 -0500
      Re: Convert a String to Int: can I use toInt() instead of Integer.parseInt()? Eric Sosman <esosman@ieee-dot-org.invalid> - 2011-07-05 13:39 -0400
        Re: Convert a String to Int: can I use toInt() instead of Integer.parseInt()? "Thee Chicago Wolf [MVP]" <.@.> - 2011-07-05 13:57 -0500
          Re: Convert a String to Int: can I use toInt() instead of Integer.parseInt()? markspace <-@.> - 2011-07-05 13:01 -0700
            Re: Convert a String to Int: can I use toInt() instead of Integer.parseInt()? "Thee Chicago Wolf [MVP]" <.@.> - 2011-07-05 15:47 -0500
              Re: Convert a String to Int: can I use toInt() instead of Integer.parseInt()? markspace <-@.> - 2011-07-05 14:57 -0700
                Re: Convert a String to Int: can I use toInt() instead of Integer.parseInt()? "Thee Chicago Wolf [MVP]" <.@.> - 2011-07-06 13:11 -0500
                Re: Convert a String to Int: can I use toInt() instead of Integer.parseInt()? markspace <-@.> - 2011-07-06 11:54 -0700
                Re: Convert a String to Int: can I use toInt() instead of Integer.parseInt()? "Thee Chicago Wolf [MVP]" <.@.> - 2011-07-06 16:58 -0500
                Re: Convert a String to Int: can I use toInt() instead of Integer.parseInt()? markspace <-@.> - 2011-07-06 15:36 -0700
                Re: Convert a String to Int: can I use toInt() instead of Integer.parseInt()? Patricia Shanahan <pats@acm.org> - 2011-07-06 16:53 -0700
                Re: Convert a String to Int: can I use toInt() instead of Integer.parseInt()? markspace <-@.> - 2011-07-06 18:07 -0700
              Re: Convert a String to Int: can I use toInt() instead of Integer.parseInt()? Nigel Wade <nmw-news@ion.le.ac.uk> - 2011-07-06 09:57 +0100
                Re: Convert a String to Int: can I use toInt() instead of Integer.parseInt()? "Thee Chicago Wolf [MVP]" <.@.> - 2011-07-06 13:13 -0500
                Re: Convert a String to Int: can I use toInt() instead of Integer.parseInt()? Nigel Wade <nmw-news@ion.le.ac.uk> - 2011-07-07 09:43 +0100
              Re: Convert a String to Int: can I use toInt() instead of Integer.parseInt()? Lothar Kimmeringer <news200709@kimmeringer.de> - 2011-07-06 11:15 +0200
                Re: Convert a String to Int: can I use toInt() instead of Integer.parseInt()? "Thee Chicago Wolf [MVP]" <.@.> - 2011-07-06 13:14 -0500
          Re: Convert a String to Int: can I use toInt() instead of Integer.parseInt()? Eric Sosman <esosman@ieee-dot-org.invalid> - 2011-07-05 16:27 -0400
            Re: Convert a String to Int: can I use toInt() instead of Integer.parseInt()? Lothar Kimmeringer <news200709@kimmeringer.de> - 2011-07-06 11:22 +0200
              Re: Convert a String to Int: can I use toInt() instead of Integer.parseInt()? Eric Sosman <esosman@ieee-dot-org.invalid> - 2011-07-06 07:33 -0400
  Re: Convert a String to Int: can I use toInt() instead of Integer.parseInt()? Eric Sosman <esosman@ieee-dot-org.invalid> - 2011-07-01 23:06 -0400
    Re: Convert a String to Int: can I use toInt() instead of Integer.parseInt()? Patricia Shanahan <pats@acm.org> - 2011-07-01 20:28 -0700
    Re: Convert a String to Int: can I use toInt() instead of Integer.parseInt()? "Thee Chicago Wolf [MVP]" <.@.> - 2011-07-05 10:27 -0500
  Re: Convert a String to Int: can I use toInt() instead of Integer.parseInt()? Roedy Green <see_website@mindprod.com.invalid> - 2011-07-04 08:59 -0700
    Re: Convert a String to Int: can I use toInt() instead of Integer.parseInt()? "Thee Chicago Wolf [MVP]" <.@.> - 2011-07-05 10:30 -0500

csiph-web