How to get String Input from User in Java


getting user input can be like the
trickiest thing when you’re first starting to learn how to code so in this
video I’m gonna teach you how to get string input from user in Java hey
it’s Alex back again helping you code quicker and easier with program examples
on this channel I make videos just like this all the time so if you’re new here
consider subscribing let’s make a new project as we always do go to file new
Java project and we’ll call it our Pokemon party cuz that’s program we’ll
be making a day hit finish open it up and go to new class and we’ll call it
our partay because it’s a partay open he’s a what is input input is anything
that the user wants to tell the computer to get input from a computer we can type
this one line of code in Java and it’s called a scanner so we’ll type scanner
it will name our scanner we’ll just call it scan and we’ll set it to a new
scanner like that and ignore these red lines we’re just gonna put system in in
here and always plan everything in a second and pretty much what this is
saying is we’re gonna get input from user with this thing called the scanner
and where are we gonna get the input from the user well we’re gonna get it
from this console right here so that’s what system name means and this is
underlined because we need to tell Java hey we’re gonna be getting input from a
user with the scanner so we’re kind of set up so let’s just ask our user or
ourselves don’t tell them that it’s just ourselves
okay we’ll tell the user to enter something
and we’ll enter our Pokemon Party because in this program we’re gonna
build our perfect party of six Pokemon so we’ll go enter Pokemon one like that
now we’ll get the input from the user and store it into a variable called
Pokemon one and that’s gonna be a string so we’ll say our string Pokemon one
equals whatever the user types in and whatever the user types in we do scan
which is our scanner object is stored in this keyword which gets it from the
console so we’ll do scan dot next line like that and this gets
the next line that the user inputs in this so when they hit enter it’ll be
starting to Pokemon away so let’s save it and run it just to see what happens
okay we’re prompted to enter Pokemon one I like
Arcanine so I’m gonna put our k9 into my party and hit enter
cool so nothing happens but Java stored this Arcanine into Pokemon one and if we
actually printed this out if we printed out Pokemon one save and run well I
gotta change it well let’s do an example let’s say Typhlosion high flows and I
think that’s how you spell it I flow Jen yeah and then we print it out so it’s in
the program now let’s just get the other five and we can just do that by copying
and pasting this a few more times no it looks so messy put some spaces and
then we’ll change it to let’s make it bigger move two three four five six and
we’ll store them in the respective variables four five and six and let’s
put that semicolon there we’ll save it and run in just to see how it’s going
let’s add that type flows in and again pokemon to have a lantern like lanterns
Flygon maybe that’s a good grass type i’ll put Meganium yeah I didn’t think
this through Tyranitar already out of rock rock dark that’s good
and then maybe a gengar oh all I accidentally hit
enter so it kind of it kind of made it mess up not cool man you’ll experience
stuff like this all the time in your program so pretty much what I did is I
hit enter and then it thought that I put nothing and then store that into Pokemon
six and then the program quit and that’s that’s what we expected
I mean it’s just kind of okay so let’s just confirm that these are the six
pokémon that you actually entered so we’ll just do that just like this will
do Pokemon one Pokemon one copy paste paste paste paste paste don’t print out
two three four five and six and then we’ll just say here are your Pokemon
here okay now here you’re poking though okay save it don’t worry if you didn’t
get all this I have the code linked in the description so you can just copy and
paste it into your IDE integrated development environment and fancy name
for Eclipse or whatever program you’re running code in will save it run it and
then go through it again so what’s another Pokemon um let’s do a flag on
Tyranitar arc and nine Charizard Wailord and what else well do chuckle
shout it to Brett and hit enter and then here are Pokemon we got fly got two
Minotaur goodnight Charizard whale or chuckle nice question of the day let me
know what six Pokemon would make up your perfect party let me know in the
comments and engage with this community of new programmers make sure to
subscribe so you see all my newest videos the day they come out and if this
was helpful smash that like button and share it if you think it might help
someone you know you could be anywhere in the world but you’re here with me and
I appreciate it you

15 Comments

Add a Comment

Your email address will not be published. Required fields are marked *