Java Programming Tutorial - 6 - Getting User Input

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
what's up guys welcome to your sixth java tutorial and in this tutorial I'm gonna be showing you a real quick way to get input from a user so one way that we can get input from a user is to use something that's already built into Java and this is called a scanner and what the scanner does is take information that the user types in from their keyboard and stores it in a variable so let's go ahead and get the scanner first and how we get it or tell Java where we need to use it is we need to import it so go ahead and write import on your first line Java jav au TI l dot scanner and this is how you're going to be inputting excuse-me importing stuff from now on so I'm just get used to that syntax if you don't know exactly what this means it's pretty much just getting the scanner and telling Java we're going to need it so of course in our class we need that public method again so in our class body go ahead and type public static void main and in your arguments type string Arg s with a square brackets what's good good enough now in your body of your main we need to actually get programming so we need to make a variable that's going to hold the input from the keyboard before you use it so to do this we need to go ahead and write something out scanner SCA and NER this means that we're going to use the scanner next you need to give your variable a name and I'm going to name mine Bucky and then you need to set it equal to whatever they enter from the keyboard so to do this you need follow-on new scanner and in your parentheses right here a man my semicolon first you need to type system dot in so here's what's going in right here or excuse me going on we told Java we need to use the scanner which is a way to get information from the keyboard we have a variable it's a scanning variable called Bucky and the Bucky variable is equal to whatever and formation that's in the keyboard and system n stands for system input and the only input we have in the computer right here is the keyboard so this pretty much is short for whatever we type in for the keyboard hold it in our Bucky variable and we're going to tell you what to do with it right now so now we have input whatever that's going to be from the keyboard and it's going to be stored in our Bucky variable what we want to do now is just print it out on the screen so you remember from the last tutorial how to do that system.out.print line and in your parenthesis you need to type pay attention the first thing you need to type is the name of your variable and that's going to be on whatever you made up here again this Bucky you don't have to write Bucky of course you can write anything you want now press Bucky period next line and what this is going to do is allow you to pause and wait until you get input from here so again let me run this program and I'll show you guys what it does go ahead and press ok and now down here is going to say a now press ENTER and it's going to output hey now so remember it had a blank cursor said hey now once you press Enter it just repeated it again and I'll show you guys what happened right there again one last time we imported this scanner if we didn't import it we can never use it since Java doesn't really like to just gather everything it only uses the things it needs it's very um I don't know Noba like it's only takes what its needs very courageous a programmable age but anyways what we did in our main function these are the only two lines you really need to look for we made a variable called Bucky and it's a scanner variable we said we're making a new scanner variable that's going to be equal to whatever the user inputted in this example hey now now hey now equals Bucky or Bucky equals hey now so what do you want so anytime you print out Bucky is going to print out hey now so instead of system.out.print line with text in here we actually change that text for Bucky next line and again what that did is pretty much take that hey now variable and put it in here and that's why it printed out hey now so that's your quick tutorial on how to use the scanner to set it equal to variable and then use that variable to output on your screen using the print line method so that's your basics on how to gather input from the keyboard and we're going to need this input to use in functions later on say we want to make a mortgage calculator or just a shopping cart that's how we do that so again I practice this just copy the source code as I do remember you don't need a name a name your function Bucky so go ahead and practice this and thank you guys for watching hopefully check out my next tutorial don't forget to subscribe to my channel and thank you and I'll see you next time
Info
Channel: thenewboston
Views: 2,283,710
Rating: 4.9229341 out of 5
Keywords: install, download, jdk, se, ee, java, development, kit, new, beginner, tutorials, eclipse, variables
Id: 5DdacOkrTgo
Channel Id: undefined
Length: 5min 31sec (331 seconds)
Published: Sat May 02 2009
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.