'How do I capture inputs from a system.out.println to compare if they match? [duplicate]

I am a first year studying app development and I have a login and registration assignment. But Im struggling to make a method where it checks if the username and password registered matches the entered login details? I am mostly using System.out.println

Here is the Login.java

package st10035771;

import java.util.Scanner;

public class Login {
    public final static int USERNAME_LIMIT = 5;
    public final static int PASSWORD_LENGTH = 8;
    private static String userName;
    private static String userPass;
    private static String message;
    private static String firstName;
    private static String lastName;
    public static Scanner un = new Scanner(System.in);

    public static boolean checkUserName(String userName) {
        if (userName.length() <= USERNAME_LIMIT && userName.contains("_")) {
            return true;
        } else {
            return false;
        }

    }

    /*
     * code attribute
     * this code was adapted from DelftStack
     * https://www.delftstack.com/howto/java/password-checker-java/
     */
    public static boolean checkPasswordComplexity(String userPass) {
        boolean isValidPassword = false;
        final int minUppers = 1;
        final int minDigits = 1;
        final int minSpecials = 1;
        int uppers = 0;
        int digits = 0;
        int specials = 0;

        for (int i = 0; i < userPass.length(); i++) {
            char ch = userPass.charAt(i);
            if (Character.isUpperCase(ch))
                uppers++;
            else if (Character.isDigit(ch))
                digits++;
            // if (ch >= 33 && ch <= 47 || ch == 64) {
            if (!Character.isLetterOrDigit(ch)) {
                specials++;
            }

        }

        if (userPass.length() >= PASSWORD_LENGTH && uppers >= minUppers && digits >= minDigits
                && specials >= minSpecials) {

            return isValidPassword = true;
        }
        return isValidPassword;
    }

    public static void inputUser() {
        System.out.println("Please enter username:");

        setUserName(un.next());

        if (checkUserName(userName)) {
            System.out.println("Username successfully captured");
        } else {
            System.out.println(
                    "Username is not correctly formatted, please ensure that your username contains an underscore and is no more than 5 characters in length.");
        }
    }

    public static void inputFName() {
        System.out.println("Please enter your name:");
        setFirstName(un.next());
        System.out.println("Please enter your surname:");
        setLastName(un.next());
    }

    public static void inputPass() {
        System.out.println("Please enter password:");
        setUserPass(un.next());

        if (checkPasswordComplexity(userPass)) {
            System.out.println("Password successfully captured");
        } else {
            System.out.println(
                    "Password is not correctly formatted, please ensure that the password contains at least 8 characters, a capital letter, a number and a special character.");
        }
    }

    public static String registerUser() {

if((checkUserName(userName)) == false){
        return "Username is not correctly formatted, please ensure that your username contains an underscore and is no more than 5 characters in length.";
    }
if((checkPasswordComplexity(userPass)) == false){
        return "Password is not correctly formatted, please ensure that the password contains at least 8 characters, a capital letter, a number and a special character";
    }

return "Welcome " +firstName+" "+lastName+",it is great to see you.";
        

    }
    
    public boolean loginUser(){
        if ((registerUser())  ==  )
        
    }

    /**
     * @param aUserPass the userPass to set
     */
    public static void setUserPass(String aUserPass) {
        userPass = aUserPass;
    }

    /**
     * @param aMessage the message to set
     */
    public static void setMessage(String aMessage) {
        message = aMessage;
    }

    /**
     * @param aFirstName the firstName to set
     */
    public static void setFirstName(String aFirstName) {
        firstName = aFirstName;
    }

    /**
     * @param aLastName the lastName to set
     */
    public static void setLastName(String aLastName) {
        lastName = aLastName;
    }

    /**
     * @param aUserName the userName to set
     */
    public static void setUserName(String aUserName) {
        userName = aUserName;
    }
}

The method in question is loginUser()



Solution 1:[1]

You have username and password as userName and userPass data members, respectively, so you will need to pass the textual data you have received from a form and compare it with your data members:

public boolean loginUser(username, password){
    if (this.userName.equals(username) && this.userPass.equals(password)) {
        //Perform the login
        return true;
    }
    return false;
}

When you call this method, based on the result (true or false you can decide what to print out). Notice that I have a comment which says "perform the login". Since it is unclear how you intend to log in I did not implement it. Note that you have a Login class, which seems to be more like an action and you need a User class. Also, making everything static is an antipattern. So, there are lots of things to improve in your code, but I'm only focusing on the aspect you have been asking the question about.

Also, System.out.println is a way to display your output to the console, you do not need to load anything back from the console. The teacher probably told you that you can output the results using System.out.println, not to read from it.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Lajos Arpad