Java Code Example: simple login

The following program represents a simple login process. The user is prompted to enter his username and password. Subsequently, the user entries are checked. If they match the stored data, the user receives the message “successfully logged in”.

import java.util.Scanner;

public class LoginExample {
    static boolean checkUserInput(String username, String password) {
        String pw = "123456", name = "user";

        if (username.equals(name) && password.equals(pw)) {
            return true;
        } else {
            return false;

    public static void main(String[] args) {
        Scanner scan = new Scanner(;
        String username, password;

        System.out.println("Enter username: ");
        username =;
        System.out.println("Enter password: ");
        password =;

        if (checkUserInput(username, password)) {
            System.out.print("Successfully logged in");
        } else {
            System.out.print("Wrong username or password");
Output first run
Enter username: 
Enter password: 
Successfully logged in
Output second run
Enter username: 
Enter password: 
Wrong username or password