 So now we get into a very important section, getting a user's input. So far we've been just throwing out code and doing mathematics on the fly. But what happens if I want to have a little user interaction, you know, I'm writing a program, I want to be able to do something with it. So to do that, we introduce a few new things. Notice over here we got a lot of stuff going on here. The first thing is we include this idea of something known as an import statement. So I've blanked out our code and the import statement, where does it go? It actually goes in the very beginning. It actually goes outside of our public class welcome. It goes well beyond here. It actually comes outside and I go import java.util.scanner. So what did I just do? Well, I mentioned in class that everything in Java is known as an object. Everything in Java is an object. This thing, this program we've been writing, this is an object. So what I've done is I've actually pulled from another what's known as library. One thing that's really nice about programming languages, especially ones that have been around for a long time is there's people better and smarter than you and me who've gone out there and built this stuff for us. Someone's already gone out and built this scanner class so that I now have an input. I now have a way to communicate to my user and get an input from them. So now what do I do? Now that I've done my import, I need to go back into my main method because this is where I have to make initialize, initialize, nice little $5 word there, initialize, create my scanner class. And I'm going to go ahead and I'll follow along with what I have on the slides. I'm going to call it scanner. Now one thing I've mentioned in the past is that scanner with the capital S and scanner with the lowercase case S, those are two separate things. Java is case sensitive so capital scanner versus lowercase scanner, those are two completely different things. If you don't like scanner, okay, why not? I'm going to call it input. Doesn't matter which way you call it. Remember, the variable is just our way of referring to it somewhere in memory. So if I say scanner input equals, remember I have to do that equals, it's the assignment operator. I've now made a place in memory for input, this is where things get a little weird because I'm now initializing an instance of an object. What initialization of an object? Just as my way of saying because scanner is not something simple like a number or a string, I have to go and create it a different way. I have to say that I want to make a new scanner of system.in. Now I said system.in because if we remember how I print a statement to the user, I say system.out. So that's my way of doing output from the user. How do I get input to the user? System.in. There's an inverse going on there. So now that I've done that, now that I have this created, I actually can now interact with my user. How? Well the first thing you do is if you notice I actually prompt the user up first. I don't just immediately say, I don't just immediately do this next line, I don't even move this up so we can kind of take a look at it, I don't immediately do scanner.next line. Because that's, they're not going to know what to type. Really? The users are dumb, they're really dumb people, they don't know what to do unless you streamline their stuff. If you take a look at our blackboard page, guess what? I streamline it for you guys, not saying you're dumb, just saying it helps you know where to go. So the first thing I'm saying is make sure to prompt your user.system.out.print and I say print that way it happens on the same line. Just a stylistic choice in my opinion, but instead of enter a number, I'm actually going to say enter a name. I'm going to just change it a little bit because why not? You already see it on the slides, let's see an application of it. Enter a name. Now I do put a space in there because remember computer is very dumb, it doesn't know to put the space there. So there we are, I've said enter a name. Now that I've prompted the user, I can type something. Let's just take a look at something for a second. If I compile this, I'm not going to get anything, but just so we can double check. We've been typing a lot, so I want to make sure everything works. Excellent, everything does work. If you did not get everything working right now, maybe take a second, pause the video, see if you can kind of debug your code. So now that I've written everything out and it's working perfectly fine, the next thing I'm going to do is I'm going to create a data type known as a string. Remember in class we said that if I need to represent text, just simple text, like the inner unnamed sentence that I have there, it's stored as a string. And I'm going to go ahead and call this, instead of input since I already use input, I know a little confusing, I'm going to call it name, string name. So in that point, it's time for me to now make my string. To do that, I have to use something called next line. Now next line, what next line does is it basically is going to wait for my user to give it an input and then it's going to hit enter. So in our case, I'm going to go ahead and wait for my user to hit enter. But I have to tell it from where? From my scanner. From that. Notice how they're both highlighted. It's because I'm referring back to my scanner object, that thing I made earlier. Next line. So now what happens is my program will hang until I hit enter. And everything that I type up until that point will get stored in the variable name. So then we'll just kind of finish things off with a system.out.println. Hello. Make sure to include your space if you're doing string concatenation, which we are doing, plus name. Now what this will do again, take a moment, go ahead, run it, see what happens. So now that you've run it, so as we kind of take a look at this, what we're going to see happen, we compile it. Excellent, ran, perfectly fine. When we say welcome.java, or welcome java, java welcome, is boom. Enter a name. It stops and it pauses. The program's not going to run any further until I hit the enter key. So whatever I type in here, such as my name, and then as soon as I hit enter, not only does the program output hello, Adam, hello, Adam, but then it closes itself, because that's the end of the program. Notice how nothing left to do, but shut down. Again, this is going to be whatever I give it. So if I say enter a name, and then I just give it a bunch of numbers. Hello, four, five, six, one, two, three, six, nine, seven. I mean, it'll do whatever you tell it to, but it's going to store it as a string. What happens if I want to turn it into a number? Well, instead of using a next line, we actually have something else. We have something called a next int. We actually have a few of these. But instead of a string, I'm going to say let's change it to an integer. And instead of next line, I say next int. Next int is now going to take whatever I say and put it in there. I'm going to do a few other tweaks as well, just because I'm dealing with a number. So I'm clearly showing math this time around. I'm going to call this enter a number. Why not? We'll call our variable number. And instead of doing hello plus number, I just want to go ahead and say number times two. We'll just say I want to see what the number is times two. So now that I've saved it and I compile it, everything worked out perfectly fine. Go ahead and take a look at this, see what happens. In our case, as soon as we do, we get enter a number. All right, well, we've prompted the user again, so I can type in a number. Don't type anything big. Remember, integers are a little too, they have a finite size to them. So I'm just going to say one, two, three. As soon as I hit enter, you see that I get 246. Let's actually take a look at that for a second. Let's see what happens when I go larger than 2.14 billion. Because remember, that's 2.14 some change is the cap of how big an integer can be. So what happens if I go, that's a 3000, that's 3 million, 3 billion. What happens if I hit enter here? Take a pause, see what happens. See if you're right. Oh, look at that, we actually crashed it. Why? Because we tried to throw too big of a number to it. So we literally stated, give me a number. And we gave it something that's not really that too big. We broke the program. So we have to be careful about this kind of stuff. So in that case, instead of doing something like an integer, we can actually make it something like a long. Now, I still have to change this. Instead of a next int, I would say I want to get the next long from my scanner. Now we do the exact same thing. We compile it, well, it compiled perfectly fine. One, two, three, thousand, million, billion. Notice how it didn't crash that time. That's because longs, unlike integers, are 64-bit long. And that means they have a little bit more control over what they can still really large numbers. So while we do have a little bit of being able to now interact with the user, we have to be a little bit careful with the user. That's actually why we prompt it, is to make sure that the user doesn't do anything too scary.