1 00:00:00,470 --> 00:00:03,460 So now we have a way of communicating with our users. 2 00:00:03,460 --> 00:00:06,960 However, our conversations seems a little one sided, doesn't it? 3 00:00:06,960 --> 00:00:08,290 Let's see if we can't go ahead and 4 00:00:08,290 --> 00:00:11,900 listen to what the person on the other side of the screen has to say. 5 00:00:11,900 --> 00:00:14,690 So what we've done thus far is all output. 6 00:00:14,690 --> 00:00:17,790 Now we're going to introduce input into our application. 7 00:00:17,790 --> 00:00:20,520 More specifically input from the keyboard. 8 00:00:20,520 --> 00:00:22,640 Let's head back over to our workspace and take some input. 9 00:00:24,230 --> 00:00:27,950 Okay, so our first named variable here is currently hardcoded. 10 00:00:27,950 --> 00:00:30,290 Let's see if we can't make it a little more dynamic. 11 00:00:30,290 --> 00:00:33,160 Remember how we said that objects like console have methods 12 00:00:33,160 --> 00:00:35,010 that allow them to perform actions? 13 00:00:35,010 --> 00:00:39,420 So far we've only seen one method, printf, and that prints text to the screen. 14 00:00:39,420 --> 00:00:42,550 Well the same console object that we've been using has another method 15 00:00:42,550 --> 00:00:44,000 called read line. 16 00:00:44,000 --> 00:00:48,020 Just like the printf method prints text out to the user, the readline method 17 00:00:48,020 --> 00:00:52,220 prints text out, and then it captures whatever the user types afterwards. 18 00:00:52,220 --> 00:00:54,170 So it can be used to prompt the user for their name and 19 00:00:54,170 --> 00:00:55,560 then capture what they type. 20 00:00:55,560 --> 00:00:56,780 Let's do that. 21 00:00:56,780 --> 00:00:58,960 So first let's remove the string. 22 00:01:02,226 --> 00:01:03,910 And then let's type console. 23 00:01:03,910 --> 00:01:07,620 And then we can access its methods by pressing a period and 24 00:01:07,620 --> 00:01:10,280 then we'll type readLine, that's the name of the method. 25 00:01:10,280 --> 00:01:16,160 And we'll call it double quotes, will type what is your name, cuz that's the prompt. 26 00:01:16,160 --> 00:01:21,470 Two spaces and then a quote, and then we will end the parenthesis, 27 00:01:21,470 --> 00:01:23,119 and we're gonna just leave that semi colon there. 28 00:01:24,250 --> 00:01:26,610 Okay, so let's take a look at this. 29 00:01:26,610 --> 00:01:29,400 So like we said, the read line method gives us a string. 30 00:01:29,400 --> 00:01:31,100 This is known as returning a value. 31 00:01:32,210 --> 00:01:35,520 We know that we can call methods on an object to perform actions 32 00:01:35,520 --> 00:01:38,730 while another feature of methods is that they can return back information when 33 00:01:38,730 --> 00:01:39,990 they're done performing. 34 00:01:39,990 --> 00:01:42,640 In this case this method returns a value that we 35 00:01:42,640 --> 00:01:45,250 then use to set our first name variable. 36 00:01:45,250 --> 00:01:49,170 Just like before, we use that value stored in the first name variable to write out or 37 00:01:49,170 --> 00:01:50,950 formatted sentences. 38 00:01:50,950 --> 00:01:55,285 Okay, so we're gonna save our file, then we are going to compile javac 39 00:01:55,285 --> 00:01:59,205 introductions.java. 40 00:01:59,205 --> 00:02:06,980 And there we're gonna go ahead and type java introductions and press Enter. 41 00:02:06,980 --> 00:02:10,630 And it's gonna prompt us know and see because up at the two spaces notice how 42 00:02:10,630 --> 00:02:13,532 the cursor is right there right after the two spaces, 43 00:02:13,532 --> 00:02:15,710 that's why it's important to leave the two spaces in the prompt. 44 00:02:15,710 --> 00:02:16,750 So we'll say, what is your name? 45 00:02:16,750 --> 00:02:19,590 And we'll put in Andrew and we'll see the program run and you'll see that it puts 46 00:02:19,590 --> 00:02:22,930 Andrew in all the places where the first name variable was just like before. 47 00:02:22,930 --> 00:02:28,120 Now the program has dynamically changed based on input from the user, nice. 48 00:02:29,390 --> 00:02:33,580 We've now learned how to take input from a keyboard in a command line program and 49 00:02:33,580 --> 00:02:35,710 then output a formatted version. 50 00:02:35,710 --> 00:02:39,930 This combination of input and output is often referred to as IO. 51 00:02:40,930 --> 00:02:44,180 In fact, if you look at the top of the code that we just wrote, 52 00:02:44,180 --> 00:02:48,700 we're importing the console type from the java.io package. 53 00:02:48,700 --> 00:02:52,650 Java packages are used to bundle related functionality and programs. 54 00:02:52,650 --> 00:02:55,630 Building and using packages is a more advanced concept but 55 00:02:55,630 --> 00:03:00,200 I just wanted to point out here that that new term that you just learned about IO 56 00:03:00,200 --> 00:03:02,720 is already right there in the statement that we had previously ignored. 57 00:03:03,820 --> 00:03:10,110 Congrats, you now know how to send output receive input, store and use variables. 58 00:03:10,110 --> 00:03:13,430 You will use these concepts in almost every interactive application 59 00:03:13,430 --> 00:03:14,760 that you'll write. 60 00:03:14,760 --> 00:03:18,160 Now since they're such important concepts, let's end this stage with an exercise.