Java Scanner Tutorial | Login and Registration Console Example

In this tutorial, we will learn how to use the Java Scanner class to read user input. We will see how to read login and registration data from console or user input.
Scanner class in Java is found in the java.util package. Java provides various ways to read input from the keyboard, the java.util.Scanner class is one of them.
The Java Scanner class breaks the input into tokens using a delimiter which is whitespace by default. It provides many methods to read and parse various primitive values.
Read more about Scanner class at
Let's first understand how the Scanner class works.

How does a Scanner work?

Basically, a Scanner breaks its input into tokens using a delimiter pattern, which by default matches whitespace (blanks, tabs, and line terminators). The parsed tokens can be converted into primitive types and Strings using various next methods. 
Here’s the simplest example of using a Scanner to read String from the user:
System.out.println("Enter string input: ");
Scanner scanner = new Scanner(;
String str = scanner.nextLine();

Java Scanner - Login Example

In the below example
  • we read userName and password input from the console
  • valid with hardcoded username and password
  • we display a login success message if a username and password matches otherwise we display an error message.

import java.util.Scanner;

public class Login {

    public static void main(String[] args) {

        try (Scanner scanner = new Scanner( {
            System.out.print(" Enter user name => ");
            String userName = scanner.nextLine();

            System.out.print(" Enter password => ");
            String password = scanner.nextLine();

            if ("ramesh".equals(userName) && "password".equals(password)) {
                System.out.println(" User successfully logged-in.. ");
            } else {
                System.out.println(" In valid userName of password ");
 Enter user name => ramesh
 Enter password => password
 User successfully logged-in.. 

Java Scanner - Registration Example

In the below example, we will see how use Scanner class to read user registration data from console and we populate all input data into Register class object.

import java.util.Scanner;

public class Registration {

    static Register register = new Register();

    public static void main(String[] args) {

        try (Scanner scanner = new Scanner( {
            System.out.print(" Enter firstName => ");
            String firstName = scanner.nextLine();

            System.out.print(" Enter lastName => ");
            String lastName = scanner.nextLine();

            System.out.print(" Enter userName => ");
            String userName = scanner.nextLine();

            System.out.print(" Enter password => ");
            String password = scanner.nextLine();

            System.out.print(" Enter emailId => ");
            String emailId = scanner.nextLine();

            System.out.print(" Enter phoneNo => ");
            long phoneNo = scanner.nextLong();


class Register {
    private String firstName;
    private String lastName;
    private String userName;
    private String password;
    private String emailId;
    private long phoneNo;
    public String getFirstName() {
        return firstName;
    public void setFirstName(String firstName) {
        this.firstName = firstName;
    public String getLastName() {
        return lastName;
    public void setLastName(String lastName) {
        this.lastName = lastName;
    public String getUserName() {
        return userName;
    public void setUserName(String userName) {
        this.userName = userName;
    public String getPassword() {
        return password;
    public void setPassword(String password) {
        this.password = password;
    public String getEmailId() {
        return emailId;
    public void setEmailId(String emailId) {
        this.emailId = emailId;
    public long getPhoneNo() {
        return phoneNo;
    public void setPhoneNo(long phoneNo) {
        this.phoneNo = phoneNo;
    public String toString() {
        return "Register [firstName=" + firstName + ", lastName=" + lastName + ", userName=" + userName + ", password=" +
            password + ", emailId=" + emailId + ", phoneNo=" + phoneNo + "]";
 Enter firstName => Ramesh
 Enter lastName => Fadatare
 Enter userName => Ramesh Fadatare
 Enter password => Password
 Enter emailId => [email protected]
 Enter phoneNo => 1234567899
Register [firstName=Ramesh, lastName=Fadatare, userName=Ramesh Fadatare, password=Password, [email protected], phoneNo=1234567899]