Java Scanner Tutorial | Login and Registration Console Example

1. Introduction

The Java Scanner class is a simple text scanner that can parse primitive types and strings using regular expressions. It is part of the java.util package and provides a great way to handle user input in console applications. This tutorial focuses on using Scanner to read user input for login and registration processes.

Key Points

- The Scanner class breaks down formatted input into tokens and translates individual tokens according to their data type.

- By default, Scanner uses whitespace as the delimiter.

- It can read data of various types using different methods like nextLine(), nextInt(), and nextDouble().

2. Program Steps

1. Set up the Scanner to read from standard input.

2. Implement the login functionality by reading username and password.

3. Implement the registration functionality by reading multiple fields from the user.

4. Handle closing the Scanner efficiently.

3. Code Program

// Login Example:
import java.util.Scanner;

public class Login {
    public static void main(String[] args) {
        try (Scanner scanner = new Scanner(System.in)) {
            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("Invalid username or password.");
            }
        }
    }
}

// Registration Example:
import java.util.Scanner;

public class Registration {
    static Register register = new Register();

    public static void main(String[] args) {
        try (Scanner scanner = new Scanner(System.in)) {
            System.out.print("Enter firstName => ");
            register.setFirstName(scanner.nextLine());
            System.out.print("Enter lastName => ");
            register.setLastName(scanner.nextLine());
            System.out.print("Enter userName => ");
            register.setUserName(scanner.nextLine());
            System.out.print("Enter password => ");
            register.setPassword(scanner.nextLine());
            System.out.print("Enter emailId => ");
            register.setEmailId(scanner.nextLine());
            System.out.print("Enter phoneNo => ");
            register.setPhoneNo(scanner.nextLong());

            System.out.println(register.toString());
        }
    }
}

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;
    }

    @Override
    public String toString() {
        return "Register [firstName=" + firstName + ", lastName=" + lastName + ", userName=" + userName + ", password=" +
            password + ", emailId=" + emailId + ", phoneNo=" + phoneNo + "]";
    }
}

Output:

Login:
Enter user name => ramesh
Enter password => password
User successfully logged-in.
Registration:
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]

Explanation:

The login example demonstrates reading a username and password from the console and comparing them against predefined values.

The registration example shows how to collect multiple types of user data and use them to populate a user registration object, showcasing how Scanner can be used to handle various input types and formats.

Comments