1.6 Accepting User Input
Join over 3 million cybersecurity professionals advancing their career
Sign up with
Required fields are marked with an *
Already have an account? Sign In »
4 hours 19 minutes
hi and welcome back to the course.
Now, in the last video, we went over some basic earth medic that we can use in her job of programs and Earth Medic that we will be in fact, using in our next video Lecter. And in this video lecture, we're gonna be talking about accepting a user input and how that exactly works.
All right, so just like the other videos, I've listed out some learning objectives for this video lecture.
So we're gonna aim to understand what user input, means and job
and understand how to accept user in but
as well as understand how we can use that user input in our programs.
So to begin, what does user input mean in Java?
It's the simple method or way of accepting input from the user.
Now, users can input different kinds of information. For example, when we go into our mobile app for banking, we input, um,
a inter juror to send money to someone, for example, or when we go to a fitness app, we can input our name or our weight, which is all a type of data. In this case, it's an intruder. But there are different types of data that a user can input
in Java and in your life.
So some of those kinds of types could be the following
ends or integers, as I mentioned previously with the bank example of floats. So floating point integers
charge, which stands for characters. As I mentioned in our earlier videos,
Boolean. So true or false answers. This could be something that we could input in a survey, for example,
and then bites
So these air all of the types of data that Caesar can infant.
So how do we actually accept user and put itself
to put it in one line, this is exactly how you do it. Now I will explain this line part by part,
So this is a line of code that allows you to accept input from the user. It will first wait for the user to enter some data or characters using the keyboard,
and then once you press enter and you're done typing, it will return whatever you typed to the Consul or the Output Council, where you can see the result of your program.
the way it accomplishes this is by using something called a scanner class, which you can see right over here after the new key word.
No, to put in a definition, a scanner class is
a package in Java, and it's used for obtaining input of primitive types like end double and many other types. And it is basically, or essentially the easiest way to read on receive input from the user. We discussed what primitive types were before and, um,
what data types the user can enter. And
these air just ah, primitive types are exactly that. And this is the line that we will be using in our programs. Accept input from the user.
Now, of course, it's always best to see an example of, um, what we mean and what We're trying to explain the concept and really grasp it.
So here is an image of a quick program that I wrote, and I'll be walking over shortly. But to add more to this, we're gonna be running this program at the beginning of the next lecture to actually show you how it works. But for now, let's just go ahead and explain each step.
So first, what it's doing is it's importing the Java dot you till the scanner
class. So what this does is
it allows the program to have all of the functionalities that you need in order to accept input from the user.
Without this line, you can't really continue because it doesn't allow you,
um, to continue because it doesn't give you the abilities that the scattered class has for you to accept input. Without this line, you cannot accept input. It's as simple as that.
Then obviously, you make a class called greeting program. We will be talking about classism or in our ex model. But this model, I wanted to focus on disk rasping some of the basics. And then what we do here is signore this line for now, um, then we move over to scanner keyboard
equals new scatter system dot in.
So if you notice, let's go back to a previous slide.
Uh, this line over here is essentially the same as
this line over here. So
what we're doing is we're making a new scanner,
and then we're giving it a name. And in this case, the name of the scanner is keyboard.
Then we're saying we're making a new scanner that Ah, that accept input from the user.
And then we have a print statement to our consul. If you remember our very first program that we world called Hello World, we also had a print statement
and also feel feel feel free to go back and watch that lecture. If you forgot some of the basic things that we discussed, then what we're doing is we're saying hello
um the value that the keyboard scanners stored. So this came, Let's say sorry. In this case, let's say your name is Salman or John. It would say, What is your name?
And you would type in your name, Then it would output. Hello, John. It just adds your name with hello and then prince it to the screen. That's essentially what this program does.
All right. So in this video lecture, we discussed the following
what user input means in Java.
How to accept user input,
how we can use that user input in our programs.
And in the next video lecture or in the next lab, I should say we will be making a small text that's very small tax program.
And the goal of this program is to essentially finish off module one and just
practice all of the concepts that we learned in module one, from variables to data types to basic class knowledge and now accepting a user input. So I look forward to
discussing and making this lab with you together in the next video
and always feel free to ask me any questions. If you have it all, you can join my Lincoln, and I would love to help you out. Thank you very much for watching. And I'll see you in the next video lecture.