1.6 Accepting User Input
Video Activity
Join over 3 million cybersecurity professionals advancing their career
Sign up with
Required fields are marked with an *
or
Already have an account? Sign In »

Video Transcription
00:00
hi and welcome back to the course.
00:02
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.
00:20
All right, so just like the other videos, I've listed out some learning objectives for this video lecture.
00:26
So we're gonna aim to understand what user input, means and job
00:31
and understand how to accept user in but
00:33
as well as understand how we can use that user input in our programs.
00:39
So to begin, what does user input mean in Java?
00:45
It's the simple method or way of accepting input from the user.
00:51
Now, users can input different kinds of information. For example, when we go into our mobile app for banking, we input, um,
01:00
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
01:19
in Java and in your life.
01:22
So some of those kinds of types could be the following
01:25
ends or integers, as I mentioned previously with the bank example of floats. So floating point integers
01:33
doubles
01:36
charge, which stands for characters. As I mentioned in our earlier videos,
01:41
Boolean. So true or false answers. This could be something that we could input in a survey, for example,
01:48
and then bites
01:51
longs
01:52
and shorts.
01:53
So these air all of the types of data that Caesar can infant.
02:00
So how do we actually accept user and put itself
02:06
so
02:07
to put it in one line, this is exactly how you do it. Now I will explain this line part by part,
02:15
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,
02:24
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.
02:38
No,
02:39
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.
02:51
No, to put in a definition, a scanner class is
02:54
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,
03:14
what data types the user can enter. And
03:17
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.
03:29
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.
03:38
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.
03:57
So first, what it's doing is it's importing the Java dot you till the scanner
04:03
class. So what this does is
04:06
it allows the program to have all of the functionalities that you need in order to accept input from the user.
04:14
Without this line, you can't really continue because it doesn't allow you,
04:18
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.
04:30
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
04:47
equals new scatter system dot in.
04:50
So if you notice, let's go back to a previous slide.
04:55
Uh, this line over here is essentially the same as
04:59
this line over here. So
05:01
what we're doing is we're making a new scanner,
05:04
and then we're giving it a name. And in this case, the name of the scanner is keyboard.
05:09
Then we're saying we're making a new scanner that Ah, that accept input from the user.
05:16
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
05:26
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
05:35
Plus
05:38
the,
05:39
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?
05:50
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.
06:03
All right. So in this video lecture, we discussed the following
06:09
what user input means in Java.
06:12
How to accept user input,
06:15
how we can use that user input in our programs.
06:18
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.
06:28
And the goal of this program is to essentially finish off module one and just
06:34
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
06:47
discussing and making this lab with you together in the next video
06:53
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.
Up Next
Similar Content