Java Scanner Tutorial

Sharing buttons:

if you're like me you may not have had a

great Java teacher and Java scanners

especially aren't one of those things

that are usually taught very well so

today I have a Java scanner tutorial for

you and if you commit to watching all

the way through

I assure you'll you'll walk away a

better Java program hey it's Alex back

again hope you're having a great day on

this channel I make a Java tutorial just

like this every single week for you so

if you're new here that's something

you're interested in then consider

subscribing to the channel we'll just

start off this Java scanner tutorial by

making a new Java project and calling it

like scanner tutorial hit finish expand

that up right click on source go to new

class and we'll call it like scanner a

touch ok hit this first check mark and

then hit finish now we're ready to start

learning about Java scanners when you

login to Facebook Twitter or Instagram

you're prompted for a username and a

password before you can get it and when

you click on the username box like your

keyboard comes up and you type in your


that's called user input when a program

asks you to enter something and you

enter it that's input to get input in

Java you can use a scanner and that's a

big reason

what scanners are used for is to get

input from a user or to get lines from a

file but today we'll focus on getting

user input from a scanner so to create

this beloved scanner that I talked so

highly about just type the word scanner

with a capital S and then name it

something it can be anything when I was

taught he likes to call it fetch because

like a dog going and fetching something

like input but I personally like to call

it scan and then do an equal sign the

keyword new and then a scanner again

with parenthesis and a semicolon way to

interact with Java and eclipse is

through this window the console window

will be typing into here and that's just

called system dot in hover over scanner

since there's some red underlines here

and then click import scanner Java dot

util that will bring the scanner code

into our program so that we can use it

and you notice it added this

line here import which is purple is a

keyword in Java dot util dot scanner

this is the set of code that contains

the scanner code and by calling import

and then this reference to it lets us

use it so if you don't have this import

statement then it won't work

your scanner won't work so you have to

have that import statement and that goes

for most things that have this format

anything that has this form at this this

equals new the same thing is called

declaring an object and an object is

just like literally any object like I

have my Nintendo switch here this is an

object like a physical object this mouse

is a physical object this scanner is a

physical object but in the computer like

on the mouse you can click you can right

click and left-click and scroll with a

scanner you can get an integer from a

user you can get a word from a user you

can get a line from a user and so we're

gonna practice using those functions and

right now we're gonna make a senior

quote program we're gonna ask a user for

their name their age and then the quote

that they want in the yearbook

so we'll first start off by asking the

user for their 8 for their name so let's

say what's your first name now without a

scanner this would be really hard for a

user to interact with this program like

what would we do when we saved and run

this nothing like we can't we can't

enter anything it has no interface

between me and the program a scanner

fixes that problem we can say to get the

name of a person we can do the name of

our scanner and then a dot to bring up

what that scanner can do and it can do

all these things so just like the mouse

can left click right click and scroll

this one can do things like closed

delimit equals find in line all these

and one of them is called next with

parentheses so this code will get the

next word that they enter so we'll save

it and

it we see what's your first name again

but now we can enter and I could say my

name Alex hit return and that's it

nothing nothing really happens but we

can what we can do is we can store what

the person entered this Alex in the

previous example we can store that into

say a string say string name equals scan

dot next now the name variable will

contain what the user entered so we have

a direct link from the input I put in to

the program and that's how websites and

apps get data from people back and forth

you enter in your keyboard it's stored

in the program you mention your keyboard

you store the program you click a

certain button it sends that information

to the program so now to make sure

everything's working we'll just print

out name the name variable which should

contain what we type in we'll save it

and run it what's your first name I'll

say like Billy enter and then we have

the Billy name returned so now we know

it's stored we're not limited to just

strings we can also get integers so we

can ask these are further age now so

we'll print out what's your age just

like we did to people if it did before

and we'll do scan dot to bring up what

the scanner functions are and it looks

like there's one called next int just

like that save it and run it you get the

first name

it's like Susan hit enter what's your

age now so I can say like 17 hit enter

and it works but we're actually not

storing that integer so that's one thing

I forgot to do so I'll just say int age

equals whatever the user entered for

that age so it's pretty cool we can get

different datatypes from a user to not

just exactly what they enter but the

type that they enter so age is a number

we can treat it as an integer and name

is a string we can treat it as a string

now the last thing we'll do here is ask

them for

their senior quote so just like before

we'll print a message asking them what's

your senior quote like that we'll try

the same thing we did up here string

quote equals scan dot next and then at

the very end we'll print out everything

we'll say like thank you and then tack

on their name which is this name

variable here to put a name inside of a

print statement with double quotes you

do a plus sign and then the variable and

then we're just gonna continue by

tacking on another string

thank you whatever your name is you are

than their age your blank years old

here I'll make this a little bigger so

you can see everything so the new print

statement since it's kind of long I'm on

line and your senior quote then tack on

that quote okay we'll save it and run it

we'll see what happens say my first name

is Alex again I currently am 22 years

old my senior quote is the sky is

falling it's like that we hit enter and

looks like it's almost ready we see

thank you Alex the input from the name

you are 22 the age we entered years old

and your senior quote is the and that

looks like it's only the first word well

why is it only getting the first word

it's because this Skanda next gets the

next word until a space character but we

want to get in this case the whole line

since a quote can be multiple words so

to do that we can just keep this say the

quote starts at the first word and then

tack on the rest of the line using

another one of the scan is function so

I'll just say that the quote is actually

the quotes which is the first word plus

scan dot next line like this and next

line will get the rest of the words

until you hit Enter and to make this a

little cleaner this is actually the same

thing as just doing plus equals plus

equals plus equals means the same thing

plus this will save it and run it and

see if everything is working properly

and don't worry if you don't understand

everything right now I'll go over

everything line by line to make sure you

understand it right after this so we'll

say my name is ace that's my dog's name

say ace wants the senior design senior

quote he is three years old

ace is three what's your senior quote um

give me or treat

please okay hit enter and also we see

thank you ace you're three years old and

your senior quote is give me more treats

please I don't know about you but that I

think that's a pretty cool Java scanner

program right there the very first thing

we do is click this green Run button and

that runs anything inside of this main

method here the first line the main

method is creating a scanner scanner the

name of the scanner equals new scanner

system in is just that console window

that we're going to be typing into we

had to hover over it and import it which

created this import Java dot util dot

scanner which just brings that scanner

code into our program next for our

senior quote program we prompted the

user for their first name

we got the input from them by doing scan

next which gets the first word and

stores that into a name variable so once

they enter their name and hit enter then

it's stored inside of the name next we

asked them for their age we do the exact

same thing they type in an age and we

store it into the age variable and

lastly we ask them for their senior

quote we get the first word of the quote

and then we get the rest of that line

and add it to the quote so the whole

quote is there and stored inside the

variable quote next we say thank you and

then we tack on their name by using plus

signs with the variable and the double

quotes we restate their age and then we

print out their senior quote and say

your senior quote is then equipped so

I'll just run it one more time to make

sure it works again is expected what's

your first name say Freddy he is like 18

and senior quote is is this the real

life or is this just fantasy ha ha and

it works as expected I hope me showing

you this has helped you learn more about

Java scanners and if you liked this job

with scanner tutorial it would help me

out a lot if you clicked that like

button and if you like my style of

videos you can hit the subscribe button

to get a new Java video from me every

single week I appreciate you being here

with me learning Java because I know

that you could be anywhere in the world

so I'm very appreciative that you here

with me so I'll catch you in the next