Getting User Input | Java | Tutorial 9

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey welcome to draft Academy my name is Mike in this tutorial I'm gonna show you how to get input from a user in Java one of the cool things about working with a programming language is we can write programs that users can interact with and any cool program is gonna allow the user to like input some information or you know they'll be able to control different aspects of the program and I'm just gonna give you guys a basic introduction into how you can allow users to affect the program by entering in information so this is gonna be a really fun tutorial and it's gonna be awesome to be able to make our programs interactive the first thing that I need to do in order to get user input is I need to create something called a scanner and a scanner is basically a special entity in Java which is going to allow users to input information into our programs so the way I create a scanner is just by saying scanner and we can give this a name so I'm gonna say keyboard input and this is gonna be the name of the scanner so we're essentially just creating like a variable here and then I want to set this equal to new scanner and inside of these parentheses right here I just want to type system dot in so let me try to explain this line a little bit what I'm doing is I'm saying scanner so I'm creating a scanner I'm calling it keyboard input and I'm setting it equal to a new scanner and inside of here I'm just putting system dot in now you'll notice here I'm getting these little red lines underneath this scanner and it says scanner cannot be resolved to a type and down here we have this little option it says import scanner java.util i'm just gonna click this and what you'll see is when I go up to the top of my file here we have this new line it says import java.util.scanner basically what's happening is here here is we're trying to use this scanner inside of our program but in order to use that scanner we have to import it into our program so I have to tell Java like hey can you go out and get the scanner for me so I can use it inside of my program but don't be too intimidated about this import statement basically all it's saying is we want to include the scanner inside of our program so that we can use it so just type out this import statement up here and now you'll be able to use this scanner once we have our scanner setup we want to prompt the user for information so I want to print something out on the screen that tells the user what I want them to input in this tutorial we'll just create a very simple little app which will ask the user for their name it'll grab the name and then we'll be able to print out hey name so the first thing I want to do is print out a prompt so I'm gonna say system dot out dot print and I'm just gonna say print here so previously in this course we've been using print Ln what println does is it'll take whatever you put inside of the parentheses it'll print it out and then it'll print a new line but in our case we just want to keep everything on the same line so I'm just gonna say print and then inside of these parentheses I can just say whatever I want to print so we'll say enter your name and actually I want to put a colon here and we'll put a space so now that we have the prompt we can actually get the input from the user so here we can use that scanner and remember I just called the scanner keyboard input I can say keyboard input dot next line and then open and closed parentheses and what this is gonna do is it's basically gonna say ok here we have this keyboard input I'm gonna wait until the user enters in a line of text so the program is going to stop here at this command and it's going to wait until the user enters in a line of text and what we want to do is we want to be able to grab that line of text and store it inside of a variable so we can create a variable over here we'll just say string name is equal to keyboard input dot next line so basically what's happening is whenever the user enters in a line of text we're gonna store whatever they enter inside of this string called name and now we'll be able to access whatever they entered by just accessing this variable so let's come down here we'll make another print statement system print and this time I'll just use print line and all I want to do is print out hey and then we'll print out the name so I'll say hey name and then we can print out like an exclamation point and so now what should happen is the user should be able to enter some text we'll store that text inside of this variable called name and then we'll print out the variable and basically say hi to the user so I'm gonna run this program you'll see over here in our console we have this prompt it says enter your name so I can just come over here and I can type in my name so I could type in Mike and now when I click enter I'll have entered a line of text so that text will now show up and it'll say hey Mike so I click enter and it says hey Mike so we're actually able to enter text into this program I'm gonna run the program again and I'll show you we can do this with another name so I could say like hey Andrea and it'll say hey Andrea so I'm typing in input the program is grabbing that input and using it to print something else out so let's do something else let's say we want the user to be able to enter in their name and also their age and this will give us a chance to use another little function so we're printing out answer your name we're grabbing the name and now I want to do something similar so I'm just gonna copy this and we'll move this onto the next line here so instead of saying enter your name I'm just gonna say enter your age and now we'll be asking the user to enter in their age so what I can do is I can actually specify to Java that we want to accept a number input so over here I'll just say age and instead of saying dot next line I'm gonna say dot next int this is basically gonna tell Java that we want to grab the next piece of input and turn it into an integer so we're expecting them to enter an in 2h and then over here I can just say int age so now we can also print out the age because it will have gotten that so we can say hey name and then we can say you are age years old so we're basically just saying hey name you are whatever years old so we're just saying how old they are so let's run this program and you'll see it has okay enter your name so I'll enter my name and enter your age so I'll enter my age and now it'll say hey Mike you are 23 years old so actually mess up this string there a little bit but you guys get the point right it says hey Mike you are whatever years old we're basically giving this program some input so over here I said keyboard input dot next line and that's because I wanted the line of text that they gave me to be converted into a string down here I said next int because I was expecting them to enter an integer so then I just said age but I could also if I want to just say next line over here but I would have to make this a string so that's the basics of getting input from a user and you can do this you know as many times as you want basically you just put out the prompt you have them enter something in you store what they entered in into a variable and then you can print it out hey thanks for watching if you enjoyed the video please leave a like and subscribe to drop acad to be the first to know when we release new content also we're always looking to improve so if you have any constructive criticism or questions or anything leave a comment below finally if you're enjoying chopped Academy and you want to help us grow head over to Draft Khadem EECOM forward slash contribute and invest in our future
Info
Channel: Mike Dane
Views: 27,447
Rating: 4.9947162 out of 5
Keywords: Programming
Id: sizFeSlGMt4
Channel Id: undefined
Length: 8min 19sec (499 seconds)
Published: Sat Oct 21 2017
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.