Java Code Example: simple login

This is a simple Java code that implements a login system. The code has a checkUserInput method that takes in a username and password and checks if it matches with the predefined values for username and password. If it matches, it returns true, otherwise it returns false.

In the main method, the code uses a Scanner to read the input from the user for the username and password. Then it calls the checkUserInput method and based on the returned value, it displays a message indicating whether the login was successful or not.

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