>>Let's talk about how to convert Strings to ints. So the problem we're looking at is
changing the String, double quote 123 double quote which has three characters in it, into
the int one hundred and twenty three. Now the difference between these two is very important
to remember. Int is a primitive data type, String is an object type. These are very different
types and you can do very different things with each. Now this is a problem that has
a lot of solutions. We're going to examine five different options here. Now the reason
that I think its good to examine different options is that sometimes one option is much
better than another. It's a very common mistake for beginning programmers: if they can think
of one way to do something, they kind of forget that sometimes there may be a much better
way out there. Now in a sense I understand, because when you're first programming it is
hard to think of all the possibilities. But it is a fun exercise and it does have more
than just intellectual merit.
Now, the way that I found the options here was by looking at the methods in the API that
have String parameters and int or integer return values. So that's where I got started.
Pay attention to the use of objects versus class methods, because that will make it much
easier for you to understand what's going on. Remember that class methods have static
in their signature.
So here's the first way. And this way is almost a cheat, because it uses something we already
know. We can actually use a String to construct a scanner object. So we could do String letters
equals new String one hundred and twenty three. Then use that String to create a scanner.
So we'll have scanner input is new scanner and then of letters. So instead of just putting
system dot in there we're putting the String letters in. Then we have int number equals
input dot next int. So that takes our scanner object input and pulls the next integer out
of it. Well the next integer is a hundred and twenty three, and that's what gets stored
in number. Here's the second way.
Let's use a class method in the integer class. Now like I said before the class methods are
labeled static. And when they when they're called they're called integer, that's the
name of the class, and then period a method name. The arguments, if there are any, go
in the parentheses. Now, there are two options that immediately spring to mind. One is int
parse int with the parameter String source, and that converts decimal only. That's actually
fine for our purposes because we're only using decimals at this point. Now there's another
method called integer decode String source. So here instead of returning an int, we are
returning an integer object. Now that's the type of object we haven't used yet, so we
might be a little bit cautious about that. One thing that's nice about decode is it can
handle decimal, hexadecimal, which is base 16, and octal, which is base 8. Now when I
say nice I mean nice for a professional programmer-- I don't really mean nice for us. In fact the
only thing we care about is that it can handle decimal.
So, which option are we going to choose? Well, if we take an integer object we're going to
have to figure out what to do with it. That's not impossible to do and in fact we're going
to do it in a few minutes. But that first one that just returns an int value, that's
really nice because we know what to do with an int. So right there is the code: Int number
equals integer dot parse int and then the argument is the String 123. You also could've
put a String reference there if you wanted to. So there's our second technique.
Now before we go onto any further techniques, let's talk a little bit about the integer
class. An integer object holds a single primitive data type int. Now, that might seem kind of
weird, and the integer class is kind of a weird class. It's something called a wrapper
class. And what it does is it wraps around a primitive data type. Now this might seem
incredibly inefficient, and it is, but it turns out it does some really cool things
later on. In order to see those cool things you're going to need to take a course like
CS 2334: Programming Structures and Abstractions. So for right now we can just think of it as
being a way of a primitive data type and moving it into an object type.
So, if we construct an integer object like integer number equals new int and then in
parentheses seven, so the argument is seven. This is what we get-- a reference to an integer
object called number and a integer object that contains a seven. Well, this brings on
the third and fourth way to perform our String and int task. We can use decode in the integer
class. So we create our String, String num is new String double quotes 123. And then
we create our integer object. So we'll have integer number, number as a reference to an
integer remember, is integer dot decode of num. Now remember integer dot decode returned
an integer object not an int type. Well we've got a little problem now. We're actually not
finished, because we need to get our int value outside of this integer object. Now that's
a method I the class if you look at the API you will find it. So what you do is this:
Int value is number dot int value. So notice number is an integer object, so we're using
the object reference period and then the method name in value. You can read about that more
in the API.
Now, as far as the fourth way, the fourth way is exactly like the third except for one
thing. There is another method in the integer class that's called value of, and you can
use that instead of decode. Other than that it works exactly the same way. Now you might
think it's kind of weird that we have so many different methods that do kind of the same
thing, but they have different purposes and they're used for different things. As you
program more this will make more and more sense. For right now it's great to know one
way to do things.
And here's the fifth and final way. We can actually construct an integer object from
the String. Now the way we know that's possible is by looking at the constructors in the Java
API page for integer. Let's actually go and do that. Here's the Java API. I'm going to
search for an integer object. Notice there are a number of classes that have the word
integer in it. We want the one that's just integer by itself and right here is the summary
of the constructors. So right here we have a second constructor that constructs a newly
allocated integer object that represents the int value indicated by the String parameter.
You will see the other methods that we've been using here. There's the decode method;
our int value method that we used a few minutes ago is here. Notice that that is not a static
method, that's an instance method. So instead of having the class name in front of it, it
will have an integer object name when it gets called. Here's the parse int method we used
earlier-- so there are lots of different possibilities there.
It's a good ting to spend some time with the API and read around it even though it can
be a little frustrating at first. We construct the integer object from the String. So String
letters equals new String and then double quote 123. And here we're constructing the
integer object from the String object. Integer number equals new integer of letters. We also
could've put double quote 123 double quote there instead of the reference to letters.
That would've been fine too. And then we'll do int value is number dot int value. So there
is our fifth way to solve this problem.
Now, the Java API has figured prominently in this discussion. All of these methods are
documented in the API. The thing is reading the API can be very challenging at first.
It's written for professional software engineers and it's not really written for beginners,
and that's unfortunate. So the critical skill that you need to develop to work with the
API is you just skip over things that don't make sense. There are corners of the API that
don't make perfect sense to me-- I just skip them until I need to know them. When I really
need to know them, that's when I work hard to know them, but a lot of the details you'll
be exposed to at first are going to be things you don't really need to know. So don't let
that stop you from reading and trying to get what you can out of it. Now the more you read
the API the better you'll understand it, and in time it will be your best friend-- just
probably not the first day. So keep programming!