0

I'm having trouble in checking for duplicates in my arraylist. Here's the actual sequence of events on how it's supposed to work:

  1. Account (Login ID of asdasd) is created and stored because arraylist is empty.
  2. If an account with the same Login ID is created again, the account will not be stored in the array, and an error will be displayed

Is there a more efficient way of checking for duplicates from class to class? Thanks a lot

Solved


Second problem: I'll fail to login if there's a string of mixed non-numeric and numeric charactes.

Scenario 1 (Failed to login): Login ID: test Password: a1s2d3f4

Scenario 2 (Failed to login): Login ID: a1s2d3f4 Password: test

Scenario 2 (Succeeded): Login ID: test Password: test

Scenario 3 (Succeeded): Login ID: asdasd123123 Password: asdasd123123

I think the problem lies with the length of the string stored in the vector. How do I make it so all data stored in the vector have a fixed length/size?

Solved


LoginPanel.class

...     
    else if (e.getSource() == registerB) {

        con = new Controller();

        String login = loginTF.getText();
        String pass = new String(passwordTF.getPassword());
        LoginRecord lr = new LoginRecord(login.trim(), pass.trim());

        int asd = con.checkRegisterData(lr);

         if(asd == 0){
            JOptionPane.showMessageDialog(null, "You've successfully created an account", "Notice", JOptionPane.INFORMATION_MESSAGE);
            loginTF.setText("");
            passwordTF.setText("");
        }

        else if(asd == 999){
            JOptionPane.showMessageDialog(null, "Please enter a Login ID or Password", "Error", JOptionPane.ERROR_MESSAGE);
        }

        else if (asd == 1){
            JOptionPane.showMessageDialog(null, "The Login ID already exists", "Error", JOptionPane.ERROR_MESSAGE);
        };


    }

    else if (e.getSource() == loginB) {

        String login = new String(passwordTF.getPassword());
        String pass = loginTF.getText();

        LoginRecord lr = new LoginRecord(login.trim(), pass.trim());

        con = new Controller();
        int asd = con.checkLoginData(lr);

        if(login.trim().isEmpty() && pass.trim().isEmpty()){    
            JOptionPane.showMessageDialog(null, "Please enter your Login ID and Password", "Error", JOptionPane.ERROR_MESSAGE);
        }

        else if(loginTF.getText().trim().equals("")){
            JOptionPane.showMessageDialog(null, "Please enter your Login ID", "Error", JOptionPane.ERROR_MESSAGE);
        }

        else if(pass.trim().equals("")){
            JOptionPane.showMessageDialog(null, "Please enter your Password", "Error", JOptionPane.ERROR_MESSAGE);
        }

        else if(asd == 0){
            main1.cardLayout.show(main1.cards, "personal");
        }

        else if(asd == 1){
            JOptionPane.showMessageDialog(null, "Please key in the correct Password or Login ID", "Error", JOptionPane.ERROR_MESSAGE);      
        }
        }   
}
}

Controller.class

    public Controller() {
    ds = new DataStorage();
}


public int checkRegisterData(LoginRecord lr) {

    // Checks to see if both text fields are empty or not
    if(lr.getLoginName().isEmpty() || lr.getPass().isEmpty()){
        result = 999;
    }
    // Array is empty
    else if(DataStorage.login.isEmpty()){
        ds.storeLoginData(lr);
        result = 0;
    }
    // There's no duplicate in the array
    else if(ds.checkLogin(lr) == false){
        ds.storeLoginData(lr);  
        result = 0;
    }       
    // There's a duplicate in the array
    else if(ds.checkLogin(lr) == true){
        result = 1;
    }
    return result;
}

public int checkLoginData(LoginRecord lr){

    boolean login = ds.checkLogin(lr);
    boolean pass = ds.checkPassword(lr);

    if (login && pass == true && true) {
        result1 = 0;
    }

    else {
        result1 = 1;
    }
    return result1;
}

}

DataStorage.class

public class DataStorage {

public static Vector<LoginRecord> login = new Vector<LoginRecord>();

public boolean checkLogin(LoginRecord lr) {

    for(int i = 0; i<login.size(); i++){
        if(login.get(i).getLoginName().equalsIgnoreCase(lr.getLoginName())){
            return true; // Duplicate exists
        }
    }
    return false;
    }


public void storeLoginData(LoginRecord lr) {
    login.add(lr);
}

public boolean checkPassword(LoginRecord lr){
    for (int i = 0; i <login.size();i++){
        if(login.get(i).getPass().equalsIgnoreCase(lr.getPass())){
            return true; // Duplicate exists
        }
    }
    return false;
}

}

4
  • 1
    why don't you use Set if you don't want duplicate entries? use LinkedHashSet to maintain the order if needed. override proper compareTo and hashCode methods to check for duplicate entries. Commented Aug 2, 2014 at 16:17
  • I've tried doing that already, but it will not stop adding accounts to the array - because it didn't find any duplicates Commented Aug 2, 2014 at 16:19
  • you might not overriding methods properly as suggested above. share your code as edit in your original post. Commented Aug 2, 2014 at 16:22
  • You will need to override hashcode in your LoginRecord class. If two LoginRecords give the same hashcode they are considered duplicates. Commented Aug 2, 2014 at 17:24

3 Answers 3

1

In your retrieveLoginData Method, you pass a String for the Logindata (Username?) and a Password (which does not make sense).

You check for equality between the Object stored in the Vector, which is a LoginRecord, and the String, which is never equal.

Also it makes no sense to have the password as a parameter, too.

Either you ask for the equality of the Username stored in the LoginRecord or you have to create a new LoginRecord from your Username and Password and ask for equality between the new object and the stored one. This would not make sense, because if someone tries to create a user with the same Username and a different Password it would be not equal as well.

Now you edited something, and you get a LoginRecord as parameter. But you are still asking for equality between a LoginRecord (login.get(i).equals) and the string lr.getLoginname

you have to make login.get(i).getLoginName.equals(lr.getLoginName)

So in total the following would be the code:

public boolean retrieveLoginData(LoginRecord lr) {

for(int i = 0; i<login.size(); i++){
    if(login.get(i).getLoginName.equals(lr.getLoginName)){
        return true;
    }
}
return false;
}

Second Problem:

Your problem is that you are checking for the loginName at your password check, where you probably want to check for the password instead.

Also problematic is, that you are checking the password and the username unrelated. If there is somebody with the Username ABC and the Password DEF and also a User with the Name 123 and the Password 456 the Users could login with both combinations (ABC:456 and 123:DEF would work)

public boolean checkLogin(LoginRecord lr) {

for(int i = 0; i<login.size(); i++){
    if(login.get(i).getLoginName().equalsIgnoreCase(lr.getLoginName())){
        return true; // Duplicate exists
    }
}
return false;
}


public boolean checkPassword(LoginRecord lr){
for (int i = 0; i <login.size();i++){
    if(login.get(i).getPass().equalsIgnoreCase(lr.getLoginName())){
        return true; // Duplicate exists
    }
}
return false;
}

What you really want to do is to ask for the Usercredentials at the same time:

public boolean checkLogin(LoginRecord lr) {

for(int i = 0; i<login.size(); i++){
    if(login.get(i).getLoginName().equalsIgnoreCase(lr.getLoginName()) &&
            login.get(i).getPass().equalsIgnoreCase(lr.getPass())){
        return true; // Duplicate exists
    }
}
return false;
}

Also, if you have time to read more about security it would be more secure to use and save the Hash of the password. But if you are just messing around and want to learn everything you should be fine with that.

Sign up to request clarification or add additional context in comments.

7 Comments

I have no idea. Show me your LoginRecord Class please, then I can help you. Maybe getLoginName is e method, then you should invoke it: login.get(i).getLoginName().equals(lr.getLoginName()). And if you want to have the same name only once with all different small and big letters make it login.get(i).getLoginName().equalsIgnoreCase(lr.getLoginName())
I think it's working now; I forgot to include the (). I'll test it a couple more times. I didn't know I had to use a getter to retrieve info from my object vector. Thanks a lot!
Loki, now the problem is that when I register and login with a string of non-numeric and numeric characters (mixed), I'll fail to log in - because the login data doesn't match with the stored data
Give me an example of the logindata, and how you check for the login
where is the numeric character? Update your Question please with the example and the corresponding source
|
0

Using a Map would allow you to store your accounts using their IDs as the key.Then you could check if an account already exists with a particular id using Map.containsKey().

2 Comments

The module that I'm taking only taught us to use vector/arraylist, but to answer your statement, it's actually my final project. How do I go about using map?
0

its very simple, use ArrayList contains()

example

ArrayList login=new ArrayList();
if(login.contains(loginTF.getText())==false)
{
login.add(loginTF.getText());
}
else
{
//show your error

}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.