Basic Authentication Program

Check username and password against hardcoded values.

BeginnerTopic: Module 2: Conditional Programs
Back

Java Basic Authentication Program Program

This program helps you to learn the fundamental structure and syntax of Java programming.

Try This Code
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        final String USERNAME = "admin";
        final String PASSWORD = "1234";

        Scanner sc = new Scanner(System.in);

        System.out.print("Enter username: ");
        String user = sc.nextLine();
        System.out.print("Enter password: ");
        String pass = sc.nextLine();

        if (USERNAME.equals(user) && PASSWORD.equals(pass)) {
            System.out.println("Login Successful");
        } else {
            System.out.println("Invalid Credentials");
        }

        sc.close();
    }
}
Output
Enter username: admin
Enter password: 1234
Login Successful

Understanding Basic Authentication Program

We compare input strings with stored credentials using equals(), demonstrating basic authentication logic.

Note: To write and run Java programs, you need to set up the local environment on your computer. Refer to the complete article Setting up Java Development Environment. If you do not want to set up the local environment on your computer, you can also use online IDE to write and run your Java programs.

Table of Contents