0

I have 4 textboxes, 2 of them being password boxes (for the user to confirm their password).

Here's my code:

public void actionPerformed(ActionEvent arg0) {
    String username = usernameField.getText();
    String emailAddress = emailField.getText();
    String password1 = passwordField1.getText();
    String password2 = passwordField2.getText();
    if (password1 != password2) {
        [code here]
    }
}

How can I make it so that if the passwords aren't equal it stops the rest of the code in that method from executing?

Makoto
  • 104,088
  • 27
  • 192
  • 230
Justin G
  • 172
  • 3
  • 19

4 Answers4

3

There is a significant problem before you can do this: you have to compare strings correctly. Then, you can decide what to do if they're false. Ultimately, using return in a void method is acceptable if you only want to return.

if (!password1.equals(password2)) {
    // [code here]
}

However, the better approach would be to do a simple blanket check, and avoid the early return - if the passwords are valid, then do operations in the scope of that block. The fall-through case would exit the method early.

public void actionPerformed(ActionEvent arg0) {
    String username = usernameField.getText();
    String emailAddress = emailField.getText();
    String password1 = passwordField1.getText();
    String password2 = passwordField2.getText();
    if (password1.equals(password2)) {
        // code on success here
    }
}
Community
  • 1
  • 1
Makoto
  • 104,088
  • 27
  • 192
  • 230
  • Thanks :) I was able to figure it out. Sorry for posting this "duplicate" or whatnot, it seems I had more than 1 issue. EDIT: Regarding the way you suggested doing it, I'd have to include a bunch of nested if statements due to the fact that I also want to check username length, password length, etc... – Justin G Feb 05 '16 at 20:17
  • @JustinG: I admit I acted a bit in haste when closing it, but generally when one sees two strings being compared with `==`, it's understandable. Glad you got it figured out. – Makoto Feb 05 '16 at 20:18
2

Never compare strings with boolean operators - Bad practice!

To solve your question just do this:

if (!password1.equals(password2)) {
    return; 
}
Maciej Cygan
  • 5,351
  • 5
  • 38
  • 72
2

How to stop the rest of code in current method from executing in Java?

I would say there are 2 ways:

  1. Let the program flow naturally to the end of the method:

    public void actionPerformed(ActionEvent e){
        if(password1.equals(password2)){
            //Do all the things if password is correct
        }            
    } //<-- If password is incorrect, point of execution will come to here 
    
  2. To exit a method before you reach the end of the method, you can use return:

    public void actionPerformed(ActionEvent e){
        if(!password1.equals(password2)){
            return;
        }
        else{
            //Do all the things if password is correct
        }
    }
    
user3437460
  • 17,253
  • 15
  • 58
  • 106
1

You can add the return statement:

return;
Makoto
  • 104,088
  • 27
  • 192
  • 230
jheimbouch
  • 969
  • 8
  • 20
  • Does this fix the comparison? – Makoto Feb 05 '16 at 20:11
  • And this is why there's a downvote: you failed to address the actual problem. Yes, returning is correct, but it's not going to work out for them if *all* they do is this. – Makoto Feb 05 '16 at 20:13
  • @cricket_007: your edit leaves a bad taste in my mouth, since it's not the answer that was originally given. – Makoto Feb 05 '16 at 20:17
  • @Makoto, can we chat? – jheimbouch Feb 05 '16 at 20:21
  • I don't have the luxury of time right now; I've just taken a brief break, but if you put a question in a chatroom that I can get to, I will answer it when I have a spare moment. – Makoto Feb 05 '16 at 20:23