Lesson 16: User Input

When creating our Java programs, we have been changing the values, compiling, and seeing the results.  Wouldn’t it be awesome if we could create programs that can ask for the user’s input and execute whatever we had programmed it to do?  Well, we are in luck!  Java has a couple of different ways to obtain a user’s input.  In this lesson, we will be learning about Java’s Scanner class!

In the Java library, there are classes that must be manually imported to each Java program in order for the class and its associated terms to be recognized and usable.  Since we are learning the Scanner class, we will be importing the Scanner class.  Let’s look at the import statement below.

In order to best understand what we are doing, let’s break it down.

import is a reserved word in Java that (as you probably guessed) is used to import a package or class.

java. refers to the package we are accessing

util. is the utility package within the Java package

Scanner specifies what class we want taken from the util package.  Finally, we close our import statement with the semicolon. 

Don’t worry, importing classes and packages will become easier the more examples we use and see.  Also, many IDEs will automatically import whatever you need.  Now that we have imported the Java scanner class, we can get to the meat and potatoes of the program.

In order to use our freshly imported Scanner, we have to create a scanner object.  In order to do this, it should look like the following:

The code circled in red is how we can create our new Scanner object!

Scanner – This tells the computer what sort of object we are working with

scan – is the user given name, it can be whatever the coder wants to name it.

new – is the reserved word that tells computer that we are creating a new instance of the object

Scanner – once again tells the computer that the object we are creating  

(System.in) – tells the computer to take the input from the keyboard/user. 

It reads similarly to:  “The Scanner (from the scanner class) that we named scan, equals a new Scanner object that takes its input from the keyboard.”  

Alright, now that we have created and named our new Scanner, we can make it turn the users input into a String by using a method we imported.  This method from the Scanner class we imported is called nextLine() and it gets (as you can guess) the next line.  

For this step, we are going to create a String called userInput and use the nextLine() method associated with our Scanner scan to obtain the user’s input.  Then, we will create a print method to reprint what was written.  Look at the code below to see the end product.

This program allows for the user to make a String, end the user input by pressing enter, and reprint the String that the user created. 

Although the program is simple, it is a good demonstration of how one can import and use the Scanner system for future projects that will require user input.  In future lessons, we will learn and use more methods associated with the Scanner class.  

GitHub link.

Leave a Reply

%d bloggers like this: