Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Well done!
You have completed Java Basics!
You have completed Java Basics!
Preview
In this video, we look to the Internet for assistance in figuring out how to retrieve an integer from a String.
Additional Reading
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
We left our program
in a bit of a broken state.
0:01
What we want to do now is ask the user
how old they are.
0:04
We already know how to prompt the user
for input, right?
0:07
We've done that before.
0:09
But there's a small problem.
0:11
The method we've been using,
readLine, always returns a string.
0:13
And as we learned, when working with
numbers, we want to use the int data type.
0:17
So how do we get an int from a string?
0:22
Let's ask the internet.
0:25
Okay, I'm going to ask Dr.
0:27
Google here, Java,
get an int from a string.
0:29
That one says also
0:37
containing letters,
so let's keep going down.
0:38
Let's try this Geeks for Geeks one here.
0:42
I'll have a link
in the teacher's notes below.
0:44
Converting a string to an int in
Java can be done using methods
0:47
provided in the integer class,
such as integer.parseInt. Hmm.
0:51
So we can see in the example
here, he's creating an int variable,
0:57
which he's just named
n, and setting that equal
1:00
to integer.parseInt s.
1:03
And up here, s is his string variable,
which contains numbers.
1:06
So it looks that's all we need to do.
1:11
Looks simple enough.
1:13
Let's go give that a try.
1:14
All right, so first we need to get
the input as a string, as we did before.
1:17
So we'll say string, age as string,
1:21
equals console dot readLine
1:26
How old are you?
1:31
Leaving a space at the end there again.
1:33
Cool, then we convert it to an int
and let's
1:36
just do it right where we set our variable
to 12 here before.
1:39
So int age will now
1:43
be equal to integer with that capital I
1:45
dot parse int
1:49
and we pass in our ageAsString variable.
1:52
Okay, let's save and see if that works.
1:58
It asks, how old are you?
2:08
I'm going to try 10.
2:10
Awesome.
2:12
The program stops
because the user is too young.
2:13
Great, that works.
2:16
Now let's try that again. Up arrow.
2:17
Remember, we're checking for
the user to be older than 13,
2:21
so let's go ahead and say they're 15,
and the program continues.
2:24
Excellent!
2:29
So if you ever want to stop
running a program like this in the terminal,
2:30
you can press control C, and it will stop.
2:33
Nice work, everyone.
2:36
Great job
using resources to find the answer.
2:38
Now you know how to parse integers
out of strings.
2:42
This is also referred
to as converting a string to an integer.
2:45
Switching between types is also often
called casting,
2:49
which we'll cover in a future course.
2:52
Also, nice job accepting the
2:56
usage of a new class, Integer,
with a capital I.
2:57
In the past, we were using the all
lowercase primitive data type int.
3:01
We used the new integer
class's static method
3:06
parseInt to generate a primitive int.
3:09
This capital I integer here is what is
known as a wrapper type or a boxed type.
3:13
And what it does for us is it provides
some methods that we can use
3:18
to manipulate and produce integers.
3:22
Remember, primitive data types
do not expose methods, so there are these
3:25
wrapper classes for every one of those
primitive data types.
3:29
Again, the other data type that we worked
with in the past was string.
3:35
We're going to need to explore
the methods available to us, because I'd
3:38
like to do something about those names
I was being called during our beta test.
3:42
But first, let's do an
exercise on parsing integers.
3:46
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up