I have 3 text fields are First Name ,Last Name and password.
First Name ,Last Name are must be characters,No empty values
password is accept all digits or character is must to equal 8
I do it but the I faced a logical error that is accept number values in First Name ,Last Name
private void save_ButtonActionPerformed(java.awt.event.ActionEvent evt) {
AdminCombobox = !buttonGroup1.isSelected(Admin_RadioButton.getModel());
SWCombobox = !buttonGroup1.isSelected(Social_RadioButton.getModel());
System.out.print(!buttonGroup1.isSelected(Admin_RadioButton.getModel()));
studentId = userID_Field.getText();
studentFirst = firstNameUser_TextField.getText();
studentLast = lasttNameUser_TextField.getText();
studentPass = password_Field1.getText();
if ( studentFirst.isEmpty() || studentLast.isEmpty() || studentPass.isEmpty()
|| AdminCombobox || SWCombobox) {//work fine
if (studentFirst.isEmpty()) {//work fine
JOptionPane.showMessageDialog(this, "First name field is empty!!", "ERROR", JOptionPane.ERROR_MESSAGE);
firstNameUser_TextField.setText("");
}
if (studentLast.isEmpty()) {//work fine
JOptionPane.showMessageDialog(this, "Last name field is empty!!", "ERROR", JOptionPane.ERROR_MESSAGE);
lasttNameUser_TextField.setText("");
}
if (studentPass.isEmpty()) {//work fine
JOptionPane.showMessageDialog(this, "Password field is empty!!", "ERROR", JOptionPane.ERROR_MESSAGE);
password_Field1.setText("");
}
if ((AdminCombobox) && (SWCombobox)) {//work fine
JOptionPane.showMessageDialog(this, "you did not choses a type of user!!", "ERROR", JOptionPane.ERROR_MESSAGE);
}
if (studentPass.length() != 8 && !studentPass.isEmpty()) {
JOptionPane.showMessageDialog(this, "length of password is not correct it must to be 8 digits or characters !!", "ERROR", JOptionPane.ERROR_MESSAGE);
userID_Field.setText("");
}
else{if (studentPass.length() == 8){
if (!containsOnlyNumbers(studentFirst)&&!containsOnlyNumbers(studentLast))//error
addUser();//to add new user
JOptionPane.showMessageDialog(this, "OK !!", "ERROR", JOptionPane.ERROR_MESSAGE);//just to if that correct or no
}else{
JOptionPane.showMessageDialog(this, "First name or last name is must to be character!!", "ERROR", JOptionPane.ERROR_MESSAGE);
userID_Field.setText("");
}
} }
}
the method I think the problem start from here,
public boolean containsOnlyNumbers(String str) {
for (int i = 0; i < str.length(); i++) {
if (!Character.isDigit(str.charAt(i)))
return false;
}
return true;
}

New Topic/Question
Reply



MultiQuote




|