大家好我正在建立一个注册表单,我想将用户名存储在ArrayList
添加用户名很好,但是当我检查重复时,它仍会添加相同的用户名
到目前为止我的代码:
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("");
}
});
}
有什么想法吗?
答案 0 :(得分:1)
每次添加set
时都会创建unameReg
。你必须创建一次。