Parsing Integers3:46 with Craig Dennis
In this video, we look to the Internet for assistance in figuring out how to retrieve an integer from a String.
So we left our program in a bit of a broken state. 0:00 What we wanna do is prompt our user to ask how old they are. 0:03 We know how to do that, as we've done that already, right? 0:06 But there's a slight problem. 0:08 The method that we've been using, read line, well, that returns a string. 0:10 But as we know now, 0:14 when dealing with numbers we want to use the integer data type. 0:15 So somehow we need to get an int from a string. 0:18 I know, how about we ask the Internet and see what we get back? 0:22 Okay, so I'm gonna go ahead and do a search for exactly what we're looking for, 0:27 which is Java get an int from a string. 0:31 And we'll take a look at what we got here. 0:38 This very first one is from an article called Stack Overflow, 0:40 which is a great resource for a developer. 0:43 Lets see if it's, indeed, what we're looking for. 0:45 It says here, how does one convert a String to an Int in Java? 0:46 That is exactly what we need. 0:50 See, I have a string which contains only numbers, and 0:52 I wanna return a number which represents it, which is what we want to do, 0:55 cuz we want to use the Int to be compared in there. 0:58 So let's go scroll down to what the answers are so 1:00 that you see there are seventeen answers, and this one has a thousand votes. 1:02 You can upvote if it worked for you, and 1:05 then there is a check mark saying that this worked for that person. 1:07 So let's see if it works for us. 1:10 So it looks like you use this integer.parseInt. 1:12 Look there's a link to the Java Doc. 1:16 Let's go ahead and click that, this is the actual Java documentation. 1:18 So let's see. 1:22 So it says that pareseInt will return an Int, and it takes a string, 1:23 which is what we have. 1:26 This looks like exactly what we want. 1:28 Let's go ahead and let's jump back over to our code. 1:30 And let's pull out the age from the line. 1:33 So we're gonna pull out a string first. 1:37 So we'll say console.readLine and we know that that gives us a sting. 1:39 We'll say how old are you? 1:43 Great. So now we have ageAsString is a string 1:47 variable, so let's go ahead and let's change this Int age that we have here, 1:52 and we'll say integer with the capital I.parseInt, 1:57 and we'll past on that age as string. 2:02 Great, and now I'm gonna save it. 2:05 And let's see if that worked. 2:07 File and run. 2:12 And it says how old are you? 2:14 So let's try and make sure that it fails and say 10. 2:15 Great, that worked. 2:18 Let's go again one more time And remember we said, so if they're older than 13. 2:20 So let's go and say they we're 15. 2:25 Excellent, it started working. 2:28 So, if you're in a program that's executing like this, 2:29 you can always Press Control-C and it will stop. 2:33 Great job leaning on the Internet. 2:37 We now know how to parse integers out of strings. 2:39 This is also referred to as converting a string to an integer. 2:42 Switching between types is also often called casting, 2:47 which we'll cover in a future course. 2:50 You also did a great job ignoring the fact that we introduced a new class integer 2:52 with a capital I. 2:57 In the past, we were using the all lowercase primitive data type Int. 2:59 We used the new integer classes static method parseInt 3:04 to generate a primitive Int. 3:08 This capital I integer here is what is known as a wrapper type, or a boxed type. 3:11 And what it does for 3:16 us is it provides some methods that we can use to manipulate and produce integers. 3:17 Remember, primitive data types do not expose methods. 3:22 So there are these wrapper classes for every one of the primitive data types. 3:26 Again, the other data type that we had worked with in the past was string. 3:30 We're going to need to explore the methods available to us because I'd also like to 3:34 do something about those names I was being called during our beta test. 3:38 But first, let's do an exercise on parsing integers. 3:42
You need to sign up for Treehouse in order to download course files.Sign up