Receiving Input3:18 with Craig Dennis
In this video, we will "listen" to the user by accepting input from the keyboard. We will then use this input to dynamically change our output to the screen.
It's not an error!
Not to worry, the Picked up JAVA_TOOL_OPTIONS: -Xmx128m is not an error on your part, we recently just tweaked our setup to have more memory allocated. Don't fret that mine looks different!
So now we have a way of communicating with our users. 0:00 However, our conversations seems a little one sided, doesn't it? 0:03 Let's see if we can't go ahead and 0:06 listen to what the person on the other side of the screen has to say. 0:08 So what we've done thus far is all output. 0:11 Now we're going to introduce input into our application. 0:14 More specifically input from the keyboard. 0:17 Let's head back over to our workspace and take some input. 0:20 Okay, so our first named variable here is currently hardcoded. 0:24 Let's see if we can't make it a little more dynamic. 0:27 Remember how we said that objects like console have methods 0:30 that allow them to perform actions? 0:33 So far we've only seen one method, printf, and that prints text to the screen. 0:35 Well the same console object that we've been using has another method 0:39 called read line. 0:42 Just like the printf method prints text out to the user, the readline method 0:44 prints text out, and then it captures whatever the user types afterwards. 0:48 So it can be used to prompt the user for their name and 0:52 then capture what they type. 0:54 Let's do that. 0:55 So first let's remove the string. 0:56 And then let's type console. 1:02 And then we can access its methods by pressing a period and 1:03 then we'll type readLine, that's the name of the method. 1:07 And we'll call it double quotes, will type what is your name, cuz that's the prompt. 1:10 Two spaces and then a quote, and then we will end the parenthesis, 1:16 and we're gonna just leave that semi colon there. 1:21 Okay, so let's take a look at this. 1:24 So like we said, the read line method gives us a string. 1:26 This is known as returning a value. 1:29 We know that we can call methods on an object to perform actions 1:32 while another feature of methods is that they can return back information when 1:35 they're done performing. 1:38 In this case this method returns a value that we 1:39 then use to set our first name variable. 1:42 Just like before, we use that value stored in the first name variable to write out or 1:45 formatted sentences. 1:49 Okay, so we're gonna save our file, then we are going to compile javac 1:50 introductions.java. 1:55 And there we're gonna go ahead and type java introductions and press Enter. 1:59 And it's gonna prompt us know and see because up at the two spaces notice how 2:06 the cursor is right there right after the two spaces, 2:10 that's why it's important to leave the two spaces in the prompt. 2:13 So we'll say, what is your name? 2:15 And we'll put in Andrew and we'll see the program run and you'll see that it puts 2:16 Andrew in all the places where the first name variable was just like before. 2:19 Now the program has dynamically changed based on input from the user, nice. 2:22 We've now learned how to take input from a keyboard in a command line program and 2:29 then output a formatted version. 2:33 This combination of input and output is often referred to as IO. 2:35 In fact, if you look at the top of the code that we just wrote, 2:40 we're importing the console type from the java.io package. 2:44 Java packages are used to bundle related functionality and programs. 2:48 Building and using packages is a more advanced concept but 2:52 I just wanted to point out here that that new term that you just learned about IO 2:55 is already right there in the statement that we had previously ignored. 3:00 Congrats, you now know how to send output receive input, store and use variables. 3:03 You will use these concepts in almost every interactive application 3:10 that you'll write. 3:13 Now since they're such important concepts, let's end this stage with an exercise. 3:14
You need to sign up for Treehouse in order to download course files.Sign up