Java User Input and Scanner Class: A Step-By-Step Guide
Java User Input and Scanner Class: A Step-By-Step Guide
Java User Input and Scanner Class: A Step-By-Step Guide
The Scanner class is used to read Java user input. Java Scanner is built into the java.util package, so no
external libraries are needed to use it. Scanner reads text from standard input. This text is returned to the
main program so it can be stored or otherwise manipulated.
In order to work with the Scanner class, you must first import it into your code. There are two ways you
can do this:
1. If you only need to work with the java.util.Scanner class, you can import the Scanner class
directly.
2. If you are working with other modules in the java.util library, you may want to import the full
library.
The following is the code for each of the above methods:
import java.util.Scanner;
import java.util.*;
The first line of code imports the Scanner class. The second line of code imports all the packages within
the java.util library, including Scanner.
It’s worth noting that there are other ways to receive user input data in Java. You can use Java’s
BufferedReader, InputStreamReader, DataInputStream, and Console classes.
However, the Scanner class is the most popular method of collecting user input in Java.
In this example, we created a variable called input that collects the next value the user inputs into the
console. Then we created a variable called number that collects the value the user submits to the
console.
class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Quantity: ");
int quantity = input.nextInt();
System.out.print("Value entered: " + quantity);
}
}
The first input we accept the name of the item. This will be a string because the item names are text-
based and use a variety of characters. In the code below, we define this string with the code: String
product_name.
The next input is the quantity of the item. This will be a number. In the code below, we define this
number with the code: int quantity, where int stands for integer.
When we run our code and insert a few example values, the program returns the following response:
Product name: 15-inch MacBook Pro 2019
Value entered: 15-inch MacBook Pro 2019
Quantity: 7
Value entered: 7
As you can see, our program collected the user’s input. It then returned to the console the value the
user entered. This allows us to verify that our program is working.
nextBoolean() Boolean
nextByte() Byte
nextDouble() Double
nextFloat() Float
nextInt() Int
nextLine() String
nextLong() Long
nextShort() Short
If you insert the wrong input type, your program will raise an InputMismatchException. For example, if
you try to insert a double into a field that collects Booleans, your program will raise an exception.
class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Quantity: ");
int quantity = input.nextInt();
System.out.print("Value entered: " + quantity);
When we run our code and insert a few example values, the program returns the following response:
Product name: 15-inch MacBook Pro 2019
Value entered: 15-inch MacBook Pro 2019
Quantity: 7
Value entered: 7
On display: true
Value entered: true
Our program works in the same way as our example above. However, this time we collect an additional
value from the user: whether the product they inserted into the program is on display. We use
the nextBoolean() method to collect this value from the user. Then we print that value to the console.
References:
https://careerkarma.com/blog/java-input/