Cameron McKenzie: Hey, I'm Cameron McKenzie, I'm
the editor in chief at theserverside.com. And I
want to quickly show you my three favorite ways to
read input from a user in a java program. And I'm
even going to throw in one more, which is my least
favorite. Now starting off, I want to show you the
Java console, you just to take input from the user
to do is say var input equals and say system dot
console, dot read line, read a line of input. And
that gets the input for you now, probably want to
prompt the user system, dot out dot print line,
type something that's always a good thing to do.
And then maybe we'll print back out to them with a
system dot out dot print line, what they typed in,
so you typed in, and then plus input. Hello, we
can maybe even say, you know, what is your name or
something like that to make it look a little bit
nicer. Now, there is a problem with the system
console class, and that is that it doesn't run on
headless Java, Java w. So if you actually try and
run this, inside of Eclipse inside of SpringSource
Tool Suite, you get this nasty error. So that is
the problem with it. I mean, there is a way to run
it, you can just say, Hey, show this in my local
terminal and then run it at the command line,
which is Java C, if you don't have to do Java C
anymore, since Java 17, the name of it Java
console, user input, dot Java, make sure you've
spelled it properly. And if you do, it says, Hey,
what's your name? My name is Cameron. you typed in
Cameron. And there you go. That's how easy it is
to take user input with the system console class
in Java. Now, where do we go after that? Well, the
big problem with the system console is that it
doesn't work in Eclipse. But you know, we've got
options. One option is to use the scanner. Now, I
do believe that you have to import the scanner,
right? You have to do import Java dot util. And I
hate doing that with people that are new to Java,
but it does make that scanner available to you.
And then all you have to do at this point is say
scanner. Scanner equals new scanner and pass
system in into the scanner. Let's just allows it
to take a feed from the console. And then I guess
we can do we can always do that old Hey, what is
your name as we did over here? And then grab the
input from the user. So say var input equals and
in this case, it would just be scanner dot next
line, I did it tutorial industry next next line,
but next line will get you everything that they
typed in. So that gives us the input. No,
previously, we just printed that evident. Hey, I'm
not. I'm all about copy and pasting previous work.
So there you go, we get the input, and we print it
out. Now watch this beautiful thing here, run as a
Java application, it runs it says what is your
name, I type in Cameron. So as you typed in
Cameron, I didn't have to do that at the command
line, I did not have to compile anything, all I
had to do was just run it. And that's one of the
benefits of the scanner. Now my third option here
is to use the J option pane. I love the J option
pane, you got to do a little import of Java x dot
swing, if you want this to work, but it is so easy
to get user input using the J option pane class, I
might copy the old What is your name here. And
actually, I don't even need to do that. All I have
to do is say var input equals say J option pain
dot show input dialog and say hey, what's your
name? So do you have to do a system dot out dot
print line? That's going to get the name and then
if I want to print out the results, all I have to
do is use that J option pain dot show message box
Show message dialog. I think the first argument is
no but I could say you typed in plus input. Okay,
that compiles gives me a false sense of security
right there but I think that's gonna work. Let's
see what happens if I do the good old run as a
Java application. Boom. It says what is your name
mapping to line seven there. I'm
going to type in Canberra and watch this boom it
says you you typed in Cameron. So there you go.
That's how you use the A J option pain to take
input from the user. Now the next option is the
system in, which I don't particularly like,
because I think it's messy, but it works. So let's
take a look at this one as well. So to make this
one work, we need to mosey on over to the Import
sections and import java.io I owe everybody
something, don't I. And then over here in the code
while to use the system in and the BufferedReader
is the way we used to do it before we had the
scanner in the console class, you have to do this
crazy stuff, you got to go input stream reader
reader equals new input stream reader and pass in
the system in Oh, boy, try explaining that whole
line of code to somebody who's new to Java. And
then after you got the input reader, you need the
buffered reader, call that b r and say New
buffered reader and pass in the reader. Okay, now
we've got that. And I guess we can do the old
prompting like we did before, we can say hey, what
is your name? And I'll just paste that in just
because I'm lazy. And then we want to do var input
equals and what are we going to get here? Well,
we'll go to that buffered reader and say read
line. So we'll read a line of input that gives us
input from the buffered reader. And then finally,
we can print out the result and much is going to
be the same as the line of code before over here,
print that out. Let's see what kind of an error
I've got here. Oh, no unhandled exception. This is
another reason why I hate to do the throws
exception up here. Or you can optionally handle
the exception if you really wanted to. But there
you go. I think that's a mess. I think that's a
scary thing to throw in Java noobs. And I also
think that's part of the reason why Java gets
flagged as being verbose because 20 years ago,
that's how you took input from the user. Now, will
this run, run as a Java application get user
input? Using this buffered reader and system in
what's your name? My name is Cameron, you typed in
Cameron. Okay, there you go. Those are the four
different ways that you can grab input from the
user read user input in a Java application using
the system in BufferedReader. I don't like that
using J option pane. That's my absolute favorite.
I love the windowing the GUI element to it, you
can always use the scanner that's fairly popular,
lots of great methods there. And I love the
console. But as you we saw the console, well
doesn't work in headless Java doesn't work with
Java w. So can't use it inside of a Eclipse or
SpringSource Tool Suite. But there you go. Those
are the different ways you can grab input from a
user in a Java application. If you enjoyed that
tutorial, why don't you head over to the server
side.com I'm the editor in chief over there. We
got lots of great tutorials on Java, Jakarta, ee,
Java, ee, Git, GitHub, DevOps micro services, you
name it, you're just my personal antics. You can
always follow me on Twitter and please sub scribe
on YouTube