1

I'm new to java and I'm trying to do a log in with a list, but the validation method does not work.

I've tried accessing the value of the list with the counter and the tried assigning it to a variable, in the debug mode the value i'm getting is correct but the condition still does not work (user: test, password test)

This is my user class with the methods to set and get the password.

public class user {
    String user;
    String pass;


    public user(String u, String p) {
        user = u;
        pass = p;

    }

    public String getUsername() {
        return user;
    }

    public String getPassword() {

        return pass;

    }

    public void setUsername(String username) {
        this.user = username;
    }

    public void setPassword(String password) {
        this.user = password;
    }
}

This is the log in class

import java.util.ArrayList;
import java.util.Scanner;

public class LogIn {
    public void Welcome() {

        System.out.println("Welcome, please enter your username");
        Scanner input = new Scanner(System.in);
         String username = input.next();
        System.out.println("Enter your password");
         String password = input.next();
         this.validate(username);

    }

    private void validate(String username){
        int attemps = 0;
        ArrayList<user> users = new ArrayList<user>();
        user User = new user("test", "test");
//Here I add the new user with the fixed value of test for password and user
        users.add(User);
        for(int i=0; i<=users.size()-1;i++){
            while (attemps<=3){
                 System.out.println(users.get(i).getUsername());
                 String test = users.get(i).getUsername();

//This condition here is the one not working and with the user already added to the list and getting the correct value in the variable it still does not work
                if(username == test){
                    System.out.println("Here");
                    attemps = 3;
                 }else{
                    System.out.println("invalid credentials");
                    attemps++;
                }}
                System.out.println("You have exceed the number of attempts");

            }
        }
    }

Using the credentials test for the user and test on the password it doesn't work

luk2302
  • 55,258
  • 23
  • 97
  • 137

0 Answers0