Java Programming Tutorial 5 - Input and Output

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello everyone welcome back this video we're gonna be talking about the basics of a Java program and we're not talking about like that conceptual garbage we've been talking about for the last four videos we're gonna start doing some hands-on work and some user input creating variables etc it's gonna be a lot of fun but you know what is even more fun than watching my videos practicing your coding interviews and you can do that thanks to our sponsor pram pram is a super legit website where you can go and practice your technical interviews such as data structures and algorithms as well as some systems design front and data science all kinds of stuff so you definitely want to go check them out if you're hoping to get a job in this field or if you just want to you know put your coding skills to the test you're gonna get paired with another individual and they're gonna ask you some questions on the fly and it's by far one of the best ways to get over that social anxiety with interviewing cuz you're now going to not only have to know how to code but you're going to have to know how to code while someone's watching you and judging all your actions and now that I think about it I'm probably not doing a very good job selling this to you guys but trust me this is gonna be one of the best ways to really solidify your knowledge and make sure you really know what you're doing with coding I've used it for practice of interviews it helped me it's tremendously useful so go check them out I'll leave a link for you guys in the description alright so moving on to the basics of java so here's how we get output so you can write stuff to the console here we've seen that in the other videos no big deal but one thing that's going to become very useful for us is getting user input and unfortunately Java's way of doing this is just a little bit more complicated than some other programming languages but it's not tremendously over-the-top complicated just for now follow my code and you'll understand it soon all right so what we're gonna do is we're gonna say scanner and then we're going to basically create a scanner and give it a name so we could call it scanner with a lowercase s and this shows something important that Java is case-sensitive so scanner with an uppercase s and scanner with a lowercase s are two separate things then we use this equal sign which is known as the assignment operator we'll talk about that in a little bit and then we say new scanner with a capital S and put some parentheses inside of these parentheses we're going to say system dot in and you can see we have an error with scanner having this red line under it if you hover over you'll get these suggestions so it's saying yo dude your scanner doesn't exist you're going to need to import that in order for it to work and there's all these different options and the one we're going to want is this Java dot util so click that and you can see if we scroll up we got this new import statement this import statement has to do with packages Java code is this is a terrible word to use but its packaged inside of these things called packages so even our class over here my sweet program inside of our package Explorer is inside of this default package we could go and create our custom packages it's basically just another way to organize your code so just how we have a class with a bunch of stuff in it well now we can have a package with a bunch of classes in it and you're probably wondering like dude why does this have to be so stinking complicated well the reason we have these packages is because like we saw if I undo this we hover over scanner here you can see that there's numerous different versions of scanner so if we didn't have these packages we would have naming conflicts because if we wanted to use this scanner here and this scanner here and this scanner here we want to be able to mix all those together if they were all in the same container we have to have a way to organize them and say hey this scanner is from Java dot util and this scanners from comm blah blah blah blah so it's just a way to organize our code one step more for now we're not going to be worrying about creating packages we're just going to be using the packages that are given to us and the way we can use them is just using these import statements and saying what package we want to use so we want to use Java dot util dot scanner now when we do this import all that it is doing is making our life easier because now it knows what scanner we're talking about if I were to comment this line out just keep it there for a second we would have to say Java dot util dot scanner and then it would work but over here we'd also have to say Java dot util dot scanner so now there's no errors but it's just a lot more verbose so what this import statement is is it just makes our life easier because we no longer have to fully qualify the entire name so the word fully qualify literally just putting the dots before it and saying exactly where the class is coming from scanner is coming from Java you too so obviously typing all these extra words is gonna kill us so we don't want to do that so you want to get rid of all those preferences we don't want to fully qualifier name my uh my puppies here to say hello now say hi copy now we just got uncomment that and we should be good to go okay so that was basically a really super long explanation of how to create a scanner now let's talk a little bit about this syntax here so we've talked about this a little bit but basically what we're doing is we're creating this variable scanner and this is the name of the variable the identifier every variable in Java needs a type so the format's going to be like this it's going to be type identifier assignment operator new type semicolon so this is the structure to create a new object so correlating that to the scanner the type is scanner the name is scanner with a lowercase s then we use the keyword new and then we say the type again which is the same as scanner and then we have these parentheses which we'll call the constructor of this class and we'll get into all that later but for now all we have to know is that when we put new and then the name and then parentheses we're calling a constructor to basically give us a new instance of this class scanner so we're creating our own scanner so just like we're creating a class here my suite program at some point someone created this scanner class and now all we want to do is we want to say yo I like the scanner class can I basically use it as a blueprint to create a new scanner and we're gonna name that scanner lower case scanner and then we're passing an argument in here system dot in which is basically just a way to say where we're getting our input from which is going to come from the console so that's how you do that now that we've got all that stuff underneath our belt let's actually try to get some input so the way we're gonna do that is we are going to basically make a string so we're gonna say string give it an identifier for example we could call a name and then we can assign it a value from the scanner by saying scanner dot next line just like that gosh there we go like I said it's a little bit complicated but this is how we get some input so what we're gonna do is we're gonna print out and say what is your name and then what we're gonna do is we're going to greet this person by doing a system out and here's a little shortcut if you types this out hold ctrl and press space boom there you go that's how you do it on a Mac I don't know how to do it on anything else sorry we're just gonna say hello put a plus sign and then name so the whole structure we're going to ask for the name we're going to create a new scanner object and use that to get a new line and then we're going to output it like so so let's run it yes save what is your name the dog my name is Caleb hello Caleb sweet so now we basically we've created a dynamic program but we no longer have to pass in command-line arguments we can just execute it so that means if we're in the terminal we don't have to say my sweet program Caleb we can just say my sweet program and it'll ask for my name and it works here is the syntax for output and here is the syntax for input now you're welcome to go to the next video of course after you check out all the links and content in the description but I did want to just go on a little rabbit trail if you're just looking for some extra information if you remember from earlier videos we talked about the concept of something being static and basically that means hey you don't have to make an instance of something well here's something that's not static this scanner dot next line is not static and the reason it's not is because we have to create a new scanner object if it was static it would look like scanner dot next line with the capital S because we'd be calling it directly on this scanner blueprint rather than on an instance of the blueprint so that's just a little side note and we're gonna talk more about that and once we get to object oriented graeme that's all I got for you guys on user input and output hopefully that was helpful for you guys and of course I will see you in the next video because you are gonna go to the next video right you're not gonna quit I'm gonna be like the rest of the world are you alright I'll catch you there peace [Music]
Info
Channel: Caleb Curry
Views: 149,392
Rating: 4.9277363 out of 5
Keywords: java programming, programming, java, tutorial, tutorial 5, input, output, beginner, java for beginners, java tutorial for beginners, caleb, curry, calebthevideomaker2
Id: JceW6zvmA_Q
Channel Id: undefined
Length: 9min 24sec (564 seconds)
Published: Mon Nov 26 2018
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.