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


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

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

From Nigel Wade <nmw-news@ion.le.ac.uk>
Newsgroups comp.lang.java.help
Subject Re: Convert a String to Int: can I use toInt() instead of Integer.parseInt()?
Date 2011-07-06 09:57 +0100
Message-ID <97imauF3tbU1@mid.individual.net> (permalink)
References (2 earlier) <k4b6175jsqa1kie5u7b7pjed29td61lkf1@4ax.com> <iuvi8e$m6g$1@dont-email.me> <7an6179km7upsvba40ebe98gmfedg2414k@4ax.com> <iuvqj8$ilj$1@dont-email.me> <dss6179oa9vq21vf7tlu6e78bdhqjdm65m@4ax.com>

Show all headers | View raw


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

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