hello everyone alex here from
wornoffkeys.com and in this java
tutorial i'm going to show you how you
can retrieve
input from the user and then store that
input into a variable
real quick before we start if you do
need help with anything feel free to
leave a comment down below
or better yet ask in the worn off keys
discord server and i'm sure
someone will be able to help you there
here i'm inside this empty main function
and i want to quickly remind you of how
to declare a variable so let's say we
have an integer
ins age equals 27. this has a few
different key components
the first part is going to be the type
of variable the second part will be the
name
and then we have an assignment operator
and so we're going to sort the value
that is to the right of the assignment
operator
into the variable to the left so in this
next line of code there's going to be
some things that i'll be explaining into
more details
later on in the series but for now just
code along and just know that it has a
similar concept
so we're going to try and create an
object of a class
which is called scanner so in order to
do this we have to first specify that
this is going to be a scanner class and
to do that we do something similar to
how we have
in right here with the type in scanner
with a capital s
now the reason why scanner is
capitalized here is because classes use
something called pascal case which means
that the first letter
of every word is capitalized this is
obviously different
than camel case which i talked about
when we talk about variables where the
first letter of every word is
capitalized aside from the very first
word
so in this case main is a class and we
see that main is capitalized
similar thing to here scanner is
capitalized now on this drop down we see
some things that intellij thinks we
might want to use
the top one is what we want and we see
this is from java.util
so this is a different package that java
came with and so this is something that
we're
accessing from the default java library
we can go ahead and press enter
and now it has created an import
statement here on line three which means
that we now have access to
java.util as a package and then scanner
is the file inside of that
then we have access to the scanner class
and now we need to name our variable
so we've defined a type similar to what
we did here with end and now the name of
our variable
can be something like input or you can
name it scanner if you want
anything that is describing it to be
what it is
now we need to use an assignment
operator and we're going to set the
value of anything to the right of the
assignment operator into
the left variable so in this case we are
going to say
new scanner with a capital s again and
intellij is automatically going to give
us these parentheses
we can then end this statement with a
semicolon i'll be explaining what the
new operator does in a future video for
now just code along
and here we see this error cannot
resolve constructor scanner
so similar to the new operator i'll be
explaining what constructors are in a
future video
for now we just have to type in system
with a capital s
dot n this line right here is going to
give us
the ability to now read and put from the
console
so we can go ahead and get rid of this
integer this was just for demonstration
purposes
and inside of here i'm going to create a
string
we can call this name and this is going
to be equal to scanner.next
i can press enter and i can add a
semicolon so scanner.next is going to
read a string so i can then print this
to the console to make sure it's working
and so instead of typing out
system.out.printline i'm just going to
use sout
and tab within intellij and i can print
out the name variable
so to make sure that this is working i
can run the program
and then here our program isn't doing
anything beforehand it would print
something to the console it would say
that it exited with code 0 or something
like that
but here it's just waiting so it's
waiting for us to enter something and so
i can enter alex
so the green text is my input and the
white text is going to be from the
actual program
so here we see that we have received
input and this was stored inside of the
variable name
and then right here we are outputting
whatever is inside the name variable
which of course is what we just inserted
now if i were to run this again
and i were to run alexzander flores with
a space here
we see that we only get the first word
the reason for that is dot next
this method right here is going to only
expect the first word
up until a white space so in order to
get the full word
or the entire line rather we're going to
use something called next
line with a capital l this is a method
and similar to variables method names
use camelcase notation which means that
the first letter of every word aside
from the first word
is capitalized so now i can go ahead and
run this
and i can enter my full name which is
alexzander flores
and here we see the full name printed
out you can also do things
other than strings so i'm going to
delete this entire line i'm going to say
int age this is going to be equal to
scanner.next
and here we see on this drop down a
bunch of different methods that we might
want to use
we say next ends byte short and byte and
short are different variable types i'll
explain further in the future
we see boolean double and float and so
we see a bunch of different options we
have
we also see next int so i'm going to
click on that and press enter
and then add in a semicolon and now this
is going to do something very similar
to the next line or the next method it's
going to expect an integer
and then we're going to store that
inside of our age variable now of course
we're no longer using name so we have to
change this to age
and then we can run the code again
now at this point i can enter 27 and
it'll print out 27
as expected now something interesting
happens if we run this one more time and
i
enter something that isn't a number so
for example alex which is obviously not
a number
if we run this we get this scary looking
message here it says
process finished with exit code one and
if you remember from previous videos
an exit code zero means that everything
went okay an exit code that is not zero
means that something went wrong
so something went wrong in this case
because we got exit code one
scrolling up we see this red message
here
input mismatch exception that is the
name of the error
because it expected an integer but we
gave it a string and so it through
this error whenever an error occurs it's
called throwing an error
and this entire thing here is called a
stack trace
now i'll be covering these in more
details in the future but for now just
know that the grayed out things
are part of the java language code and
we don't have to worry about them
but if we scroll down we see this right
here com.wornoffkeys
and you'll likely see your own package
name dot main which is the class that we
are in
as we see right here and then dot main
with lower case which is the method you
were in which we see right here on line
six
now here we see main.java colon nine
that nine is the line number if we look
here at line nine
this is where we're trying to return the
integer from the user input
now i give it a string and so this is
where the error occurred
now note that i clicked here on line 13
and if we click on this blue right here
it's going to jump my cursor to line 9.
if we had this closed
and we clicked on it it would then open
it up so this is more useful in larger
projects where you have dozens of code
files and hundreds of lines of code
and you can then click on where the
error is and intellij will jump you
right to that part
obviously in this three line code
project here it's not going to be that
much of a benefit
but this is how you would jump to the
part of the code that's giving you
problems
now at this stage we know this is the
problem and so we can actually run this
again
and we can now enter an integer such as
27 and we get 27.
in the future i'll show you how to
handle those errors and then provide a
meaningful message to the user
such as please enter a valid number but
that's for another video
so speaking of providing useful
information to the user
we're expecting the user to know exactly
what to do we're actually not sending a
message at all
and so we can do that above our scanner
declaration i'm going to make a new line
i'm going to say s-o-u-t and press tab
and i'm going to say please
enter a number and then we can run this
and it's going to ask us to enter our
number so then i can enter 27.
now note that here i'm typing on a line
below it and of course this will still
work
but what if we want to type on the same
exact line directly after the message
well print line is going to print the
line here that we want the text
and then it's going to print out a new
line so you can get rid of the ln
and now this is a different method
that's just called print
that is not going to add in a new line
afterwards to test this we can click on
run
and then i can enter 27. now note that
we are right against the colon here and
that looks a little weird
so of course this still works but we can
now add a space directly after the colon
and if we run this one more time we can
now enter 27
and it seems like a lot cleaner input
there's a space in between the colon and
the number
so this is how you're going to retrieve
basic user input using java
in the next video i'm going to show you
how you can use arithmetic operators
to add and subtract and do basic math
things within
your integers that you're receiving from
the user thanks for watching this java
tutorial
if you need help leave a comment down
below or ask in the worn off keys
discord which will be linked in the
video description
if you want access to the source code
for this video a link to the github
repository can be found
in the video description as well if you
found this video helpful
please consider liking and subscribing
with the notifications on so you get
notified whenever i release a future
java tutorials
