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:
- Account (Login ID of asdasd) is created and stored because arraylist is empty.
- 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;
}
}
Setif you don't want duplicate entries? useLinkedHashSetto maintain the order if needed. override propercompareToandhashCodemethods to check for duplicate entries.