Scanner Class in Java

The Java.util.Scanner class is a standard class in Java and is used to read inputs of primitive data types. It is the easiest way to read the information provided by the user.

How to submit input to your program in Java?

Sometimes you want to submit input data to your program. It may be an entry of a name or the number of products that someone has purchased in a store. We can use Java’s built-in class Scanner to handle these inputs. For example, scanners can take input from the keyboard, files, or text boxes.

 

 

We can use Java’s built-in class Scanner to handle user input.

Scanner Class in Java

HOW DO YOU USE THE SCANNER CLASS IN JAVA?

The scanner reads, among other things, values that the user enters into the terminal. To use the scanner class in Java, we first need to import the library by writing

 

import java.util.Scanner;

 

Furthermore, we also need to create a scanner object. In later chapters, we will go through what class and objects are in Java, but for the now, let’s learn how to collect information from the user with the help of a scanner object.

 

We will create the scanner object by writing

 

// import the scanner class
import java.util.Scanner; 

// Declare a scanner object 
Scanner input = new Scanner(System.in);

 

  • We have created a scanner object that we have named input (you can, of course, choose another name as well)
  • Note that we also need to write System.in within the parentheses to be able to read the input

Example: Using the Scanner Class in Java

With a Scanner, the user can enter any information in the terminal window. However, this can be problematic, since the user, for example, can submit a text string when we are requesting an integer. In the page on data types in Java, we learned that our program would crash if we try to save the wrong kind of information in the wrong data type. Therefore, we must specify what data type we will send to the scanner.

 

Let’s have a look at an example

  • First, we create a simple program that will ask the user to provide two integers
  • Secondly, we add the two numbers
  • Finally print the answer

 

We assume that the user knows to only provide integers and that the user does the input correctly.

 

Writing this in code

 

import java.util.Scanner;  

public class Scanner_example {
    public static void main(String[] args) {

        Scanner input = new Scanner(System.in);

        System.out.println("Enter first number: ");
        // Save the first number
        int first = input.nextInt();                

        System.out.println("Enter second number: ");
        // Save the second number
        int second = input.nextInt();              

        // Prints first, second and first + second number
        System.out.println(first + " + " + second + " = " + (first+second));
    }
}

MANAGE THE RIGHT DATA TYPE AS INPUT for Scanner Class in Java

 

Furthermore, it is critical to specify which data type the user will enter. Otherwise, the Scanner will not be able to handle the data. The following tables show some methods we can use with the Scanner object to handle each data type.

 

Numbers

Method Info Example
Int nextInt() Saves the next input as an int,
will be an error if it is not an int.
input.nextInt();
Double nextDouble() Saves the next input as an double,
will be an error if it is not an double.
input.nextDouble();

 

Characters

Method Info Example
String next() Returns the next character
from the scanner
input.next();
String nextLine() Returns the next line as a String input.nextLine();

 

Boolean (True or False)

Method Info Example
boolean hasNext() Returns true if there is more
to retrieve, otherwise false
input.hasNext();
boolean hasNextInt() Returns true if the next
element the scanner can be
interpreted as an integer (int),
otherwise false
input.hasNextInt();
boolean hasNextDouble() Returns true if the next
element the scanner can be
interpreted as an double,
otherwise false
input.hasNextDouble();

More information about the Scanner Class in Java is available at the Oracle website