1 00:00:00,200 --> 00:00:03,240 So we left our program in a bit of a broken state. 2 00:00:03,240 --> 00:00:06,820 What we wanna do is prompt our user to ask how old they are. 3 00:00:06,820 --> 00:00:08,830 We know how to do that, as we've done that already, right? 4 00:00:08,830 --> 00:00:10,470 But there's a slight problem. 5 00:00:10,470 --> 00:00:14,400 The method that we've been using, readLine, well, that returns a String. 6 00:00:14,400 --> 00:00:15,260 But as we know now, 7 00:00:15,260 --> 00:00:17,650 when dealing with numbers we want to use the Integer data type. 8 00:00:18,690 --> 00:00:21,730 So somehow we need to get an int from a String. 9 00:00:22,850 --> 00:00:25,790 I know, how about we ask the Internet and see what we get back? 10 00:00:27,450 --> 00:00:31,470 Okay, so I'm gonna go ahead and do a search for exactly what we're looking for, 11 00:00:31,470 --> 00:00:36,760 which is Java get an int from a string. 12 00:00:38,760 --> 00:00:40,170 And we'll take a look at what we got here. 13 00:00:40,170 --> 00:00:43,140 This very first one is from a website called Stack Overflow, 14 00:00:43,140 --> 00:00:45,050 which is a great resource for a developer. 15 00:00:45,050 --> 00:00:46,760 Lets see if it's, indeed, what we're looking for. 16 00:00:46,760 --> 00:00:50,610 It says here, how does one convert a String to an Int in Java? 17 00:00:50,610 --> 00:00:52,770 That is exactly what we need. 18 00:00:52,770 --> 00:00:55,480 See, I have a string which contains only numbers, and 19 00:00:55,480 --> 00:00:58,100 I wanna return a number which represents it, which is what we want to do, 20 00:00:58,100 --> 00:01:00,390 cuz we want to use the Int to be compared in there. 21 00:01:00,390 --> 00:01:02,550 So let's go scroll down to what the answers are so 22 00:01:02,550 --> 00:01:05,880 that you see there are seventeen answers, and this one has a thousand votes. 23 00:01:05,880 --> 00:01:07,630 You can upvote if it worked for you, and 24 00:01:07,630 --> 00:01:10,590 then there is a check mark saying that this worked for that person. 25 00:01:10,590 --> 00:01:12,350 So let's see if it works for us. 26 00:01:12,350 --> 00:01:16,340 So it looks like you use this Integer.parseInt. 27 00:01:16,340 --> 00:01:18,050 Look there's a link to the Java Doc. 28 00:01:18,050 --> 00:01:22,010 Let's go ahead and click that, this is the actual Java documentation. 29 00:01:22,010 --> 00:01:23,240 So let's see. 30 00:01:23,240 --> 00:01:26,810 So it says that parseInt will return an int, and it takes a String, 31 00:01:26,810 --> 00:01:28,140 which is what we have. 32 00:01:28,140 --> 00:01:30,610 This looks like exactly what we want. 33 00:01:30,610 --> 00:01:32,730 Let's go ahead and let's jump back over to our code. 34 00:01:33,900 --> 00:01:37,750 And let's pull out the age from the line. 35 00:01:37,750 --> 00:01:39,400 So we're gonna pull out a String first. 36 00:01:39,400 --> 00:01:43,115 So we'll say console.readLine and we know that that gives us a String. 37 00:01:43,115 --> 00:01:45,580 We'll say how old are you? 38 00:01:47,980 --> 00:01:52,580 Great. So now we have ageAsString is a String 39 00:01:52,580 --> 00:01:57,220 variable, so let's go ahead and let's change this int age that we have here, 40 00:01:57,220 --> 00:02:02,341 and we'll say Integer with the capital I .parseInt, 41 00:02:02,341 --> 00:02:05,610 and we'll paste on that ageAsString. 42 00:02:05,610 --> 00:02:07,730 Great, and now I'm gonna save it. 43 00:02:07,730 --> 00:02:08,898 And let's see if that worked. 44 00:02:12,162 --> 00:02:13,040 File and run. 45 00:02:14,190 --> 00:02:15,590 And it says how old are you? 46 00:02:15,590 --> 00:02:18,125 So let's try and make sure that it fails and say 10. 47 00:02:18,125 --> 00:02:20,030 Great, that worked. 48 00:02:20,030 --> 00:02:25,140 Let's go again one more time And remember we said, so if they're older than 13. 49 00:02:25,140 --> 00:02:28,150 So let's go and say they we're 15. 50 00:02:28,150 --> 00:02:29,320 Excellent, it started working. 51 00:02:29,320 --> 00:02:33,660 So, if you're in a program that's executing like this, 52 00:02:33,660 --> 00:02:35,780 you can always press Control-C and it will stop. 53 00:02:37,190 --> 00:02:39,100 Great job leaning on the Internet. 54 00:02:39,100 --> 00:02:42,620 We now know how to parse Integers out of Strings. 55 00:02:42,620 --> 00:02:46,220 This is also referred to as converting a String to an Integer. 56 00:02:47,330 --> 00:02:50,390 Switching between types is also often called casting, 57 00:02:50,390 --> 00:02:52,480 which we'll cover in a future course. 58 00:02:52,480 --> 00:02:57,470 You also did a great job ignoring the fact that we introduced a new class Integer 59 00:02:57,470 --> 00:02:59,300 with a capital I. 60 00:02:59,300 --> 00:03:03,170 In the past, we were using the all lowercase primitive data type int. 61 00:03:04,260 --> 00:03:08,590 We used the new Integer class's static method parseInt 62 00:03:08,590 --> 00:03:10,120 to generate a primitive int. 63 00:03:11,120 --> 00:03:15,640 This capital I Integer here is what is known as a wrapper type, or a boxed type. 64 00:03:16,770 --> 00:03:17,730 And what it does for 65 00:03:17,730 --> 00:03:21,590 us is it provides some methods that we can use to manipulate and produce Integers. 66 00:03:22,590 --> 00:03:26,100 Remember, primitive data types do not expose methods. 67 00:03:26,100 --> 00:03:29,100 So there are these wrapper classes for every one of the primitive data types. 68 00:03:30,480 --> 00:03:34,990 Again, the other data type that we had worked with in the past was String. 69 00:03:34,990 --> 00:03:38,770 We're going to need to explore the methods available to us because I'd also like to 70 00:03:38,770 --> 00:03:42,220 do something about those names I was being called during our beta test. 71 00:03:42,220 --> 00:03:45,595 But first, let's do an exercise on parsing Integers.