How to Read User Input with the Scanner Class in Java


In this article, we show how to read user input with the Scanner class in Java.

So below we show the full Java code for reading a string that a user inputs.

So this is the code shown above that allows a user to enter input using the Scanner class.

We'll now go through the code line by line so that you can understand each line.

In the first line, we import the Scanner class. Before you can use the Scanner class in a program, you must import it. Do tha, you use an import statement at the beginning of the program before the class declaration.

The line shown above imports the Scanner class from the java.util package. If you just want to important all the classes from the java.util package, you can instead write the statement, import java.util.*;

import java.util.*; imports all the classes in the java.util package, including the Scanner class. So you probably will encounter this in Java programming.

In the next statement, we create a public class called UserInput.

Inside of this class, we declare and create a Scanner object.

Before you can use the Scanner class to read input from the console, you must declare a Scanner variable and create an instance of the Scanner class.

This is done through the line, static Scanner p= new Scanner(;

With this line, we create an instance of the Scanner class named p. We then specify it as,, which indicates that it's a standard keyboard console input.

The next line declares the main function(), which every Java program must have.

We then write out, "Enter an integer" through the System.out.print function.

We create an integer variable named numberentered and set it equal to the Scanner object with the method .nextInt(). This numberentered variable stores the integer that the user enters.

To read an input value from the user, you use one of the methods in the Scanner class. These are all shown in the table listed below. Since we are retrieving an integer value from the user, we use the Scanner method nextInt() to get the integer value. If we were retrieving a double value, we would use the Scanner nextDouble() function. If we were retrieving a string, we would use the Scanner nextLine() function. Again, see the table below to retrieve data for a given variable type.

So once we have retrieved this data using the Scanner method, we then store it in the numberentered variable.

Then we output what the user entered using the System.out.println function.

And this ends our code.

The list of Scanner methods available for the different variable types is shown in the table below.

Variable Type Scanner Class Method to Retrieve Data Explanation
boolean nextBoolean reads a boolean value from the user
byte nextByte() reads a byte value from the user
double nextDouble() reads a double value from the user
float nextFloat() reads a float value from the user
int nextInt() reads an int value from the user
String nextLine() reads a String value from the user
long nextLong() reads a long value from the user
short nextShort() reads a short value from the user

Again, you would use the appropriate Scanner method pertaining to the variable type we are trying to get from the user. If an int, you use nextInt(). If a float, you use nextFloat().

And this is how we can read user input with the Scanner class.

Related Resources

How to Read Input with the JOptionPane Class in Java

HTML Comment Box is loading comments...