I'm trying to make a program where the user needs an account to access other parts of it. I want this set up so that if the user's 2 confirm password dont match, they have to reenter the info. Also, if the user leaves anything blank, they must reenter all the info. How can I do this so it continues to loop until the user enters good info?
if (e.getSource() == okButton) {
if(!passString.equals(passStringConfirm) || userName.equals(null) || passString.equals(null) || passStringConfirm.equals(null)){
enterUsername.setText("");
enterPassword.setText("");
enterConfirmPassword.setText("");
}
}
This is what I have so far, and if only works for one iteration. I tried do while and it would continually print out the warning message I was trying to print out in a JOptionPane.
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.border.Border;
public class CreateAccount extends JFrame implements ActionListener {
JLabel username = new JLabel("Enter your username");
JTextField enterUsername = new JTextField(null, 15);
JLabel password = new JLabel("Enter your password");
JPasswordField enterPassword = new JPasswordField(null, 15);
JLabel passwordConfirm = new JLabel("Confirm your password.");
JPasswordField enterConfirmPassword = new JPasswordField(null, 15);
JButton okButton = new JButton("OK");
String userName;
double initialDeposit;
public CreateAccount() {
add(username);
add(enterUsername);
add(password);
add(enterPassword);
add(passwordConfirm);
add(enterConfirmPassword);
add(okButton);
okButton.addActionListener(this);
setTitle("New Bank Account Creation");
setVisible(true);
setLocationRelativeTo(null);
setSize(270, 300);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLayout(new FlowLayout());
}
@Override
public void actionPerformed(ActionEvent e) {
char[] pass = enterPassword.getPassword();
String passString = new String(pass);
char[] passConfirm = enterConfirmPassword.getPassword();
String passStringConfirm = new String(passConfirm);
userName = enterUsername.getText();
if (e.getSource() == okButton) {
if(userName == null || userName.isEmpty() || passString == null || passString.isEmpty() || !passString.equals(passStringConfirm)) {
enterUsername.setText("");
enterPassword.setText("");
enterConfirmPassword.setText("");
Border redLine = BorderFactory.createLineBorder(Color.red);
enterUsername.setBorder(redLine);
enterPassword.setBorder(redLine);
enterConfirmPassword.setBorder(redLine);
repaint();
}
}
super.dispose();
int response = 0;
String firstDesposit = JOptionPane.showInputDialog("Welcome " + userName + ". Enter your initial deposit.");
initialDeposit = Double.parseDouble(firstDesposit);
if (response == JOptionPane.OK_OPTION) {
new Menu();
}
}
}
Your if
test cannot be correct. If any of your String
(s) are null
you would get a NullPointerException
. I think you wanted
if (userName == null || userName.isEmpty() || passString == null
|| passString.isEmpty()
|| !passString.equals(passStringConfirm)) {
enterUsername.setText("");
enterPassword.setText("");
enterConfirmPassword.setText("");
}
Then your UI code should check if those are empty before allowing the user to proceed. Finally, in the code above I believe you might use setBorder()
to give those fields a red border.
if (userName == null || userName.isEmpty() || passString == null
|| passString.isEmpty()
|| !passString.equals(passStringConfirm)) {
Border redLine = BorderFactory.createLineBorder(Color.red);
enterUsername.setText("");
enterPassword.setText("");
enterConfirmPassword.setText("");
enterUsername.setBorder(redLine);
enterPassword.setBorder(redLine);
enterConfirmPassword.setBorder(redLine);
}
Edit
Based on the code you provided, but you need it in an else!
if(userName == null || userName.isEmpty() || passString == null
|| passString.isEmpty() || !passString.equals(passStringConfirm)) {
enterUsername.setText("");
enterPassword.setText("");
enterConfirmPassword.setText("");
Border redLine = BorderFactory.createLineBorder(Color.red);
enterUsername.setBorder(redLine);
enterPassword.setBorder(redLine);
enterConfirmPassword.setBorder(redLine);
repaint();
} else { // <-- add this
super.dispose();
int response = 0;
String firstDesposit = JOptionPane.showInputDialog(
"Welcome " + userName + ". Enter your initial deposit.");
initialDeposit = Double.parseDouble(firstDesposit);
if (response == JOptionPane.OK_OPTION) {
new Menu();
}
}