简体   繁体   中英

Check if username exists in Arraylist

Hello guys i am building a registration form and i want to store the usernames in an ArrayList

Adding the username is fine but when i check for duplicates it still adds the same username

My code so far:

private void clickEventLeftPanel() {
        registerBtn.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                unameReg = registerUser.getText();
                regPass = registerPassword.getPassword();
                regRetype = registerRetype.getPassword();
                set = new ArrayList();
                if (unameReg.isEmpty()) {
                    JOptionPane.showMessageDialog(HomeAssignmentJavaLab.this, "Username is Required", "Error", JOptionPane.ERROR_MESSAGE);
                } else if (regPass.length == 0) {
                    JOptionPane.showMessageDialog(HomeAssignmentJavaLab.this, "Password is Required", "Error", JOptionPane.ERROR_MESSAGE);
                } else if (regPass.length < 6) {
                    weakness.setText("Very Weak");
                    weakness.setBackground(Color.red);
                    JOptionPane.showMessageDialog(HomeAssignmentJavaLab.this, "Your password is very weak please enter a more complex password", "Error", JOptionPane.ERROR_MESSAGE);
                } else if (regPass.length < 8) {
                    weakness.setText("Moderate");
                    JOptionPane.showMessageDialog(HomeAssignmentJavaLab.this, "Your password is moderate please enter a more complex password", "Error", JOptionPane.ERROR_MESSAGE);
                    weakness.setBackground(Color.yellow);
                } else if (regRetype.length == 0) {
                    JOptionPane.showMessageDialog(HomeAssignmentJavaLab.this, "Please retype your password", "Error", JOptionPane.ERROR_MESSAGE);
                } else if (regPass != regRetype && regPass.length != regRetype.length) {
                    JOptionPane.showMessageDialog(HomeAssignmentJavaLab.this, "Passwords do not match", "Error", JOptionPane.ERROR_MESSAGE);
                } else {
                    weakness.setText("Strong");
                    weakness.setBackground(Color.green);
                    if(set.contains(unameReg)){
                        System.err.println("Already exists");
                    }else{
                        set.add(unameReg);

                    }
                    JOptionPane.showMessageDialog(HomeAssignmentJavaLab.this, "Registration Successful user " + registerUser.getText() + " was entered", "Success", JOptionPane.PLAIN_MESSAGE);
                }

                weakness.setText("");
                weakness.setBackground(getBackground());
                registerUser.setText("");
                registerPassword.setText("");
                registerRetype.setText("");
            }


    });
    }

Any ideas?

You create set every time you add unameReg . You have to create it once.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM