1

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();
            }
        }
    }
Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
AlecR
  • 47
  • 2
  • 9
  • 5
    On a button click? That would lock the UI. Also **nothing** `.equals(null)`, if it's `null` you'd get a `NullPointerException` when you invoked the method `equals`. – Elliott Frisch Nov 13 '14 at 22:31
  • The loop is what I need help implementing. I tried a do while with a JOptionPane message saying the error of the user, but it would continually print when I closed the window out. How can I make this loop function while only printing the JOptionPane warning once? – AlecR Nov 13 '14 at 22:38

2 Answers2

0

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();
    }
}
Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
  • how do I avoid the frame from locking up? – AlecR Nov 13 '14 at 23:03
  • I figured out that a while loop causes it to freeze up. how can I do this so that the window appears again without locking up? – AlecR Nov 14 '14 at 02:42
  • @AlecR You cannot do it with a loop. You must implement a method in the UI (which you have not posted) to validate the password before allowing the login screen to move on. All you have posted is the callback handler for a button click and looping on a button click event *will* lock the UI. – Elliott Frisch Nov 14 '14 at 02:45
  • I've added all the code above. What would this method look like? I'm unsure of how you would do this – AlecR Nov 14 '14 at 02:48
  • You've added no code at all. Your button click listener handles button clicks. How many times can the user click your button? Where do you set-up your buttons? Where do you add the listener to the button? – Elliott Frisch Nov 14 '14 at 02:49
  • Isn't that all in the code I attached at the bottom of my original question just now? All the code for setting up my buttons is in there. – AlecR Nov 14 '14 at 02:52
  • @AlecR No. You edited my answer, I took the code and edited your question. Anyway, all you need now is an `else`. I edited my answer above. By the way, you newest code includes my answer. – Elliott Frisch Nov 14 '14 at 02:55
  • I realize what you were asking now, sorry for the confusion! I'm not used to the format of this website yet. Thank you for the help the else did the trick! Only issue now is that when I correct my field so they work I can't click the ok button again, but I should hopefully be able to figure this issue out. – AlecR Nov 14 '14 at 02:59
-1

Assuming e.getSource() is a blocking call.

while(true){
if (e.getSource() == okButton) {
        if(!passString.equals(passStringConfirm) || userName.equals(null) || passString.equals(null) || passStringConfirm.equals(null)){
                enterUsername.setText("");
                enterPassword.setText("");
                enterConfirmPassword.setText("");
                }
            }
else
   break; //correct password

}
PUG
  • 4,301
  • 13
  • 73
  • 115