Program-Me-Rev
Program-Me-Rev

Reputation: 6624

How to efficiently check if two Strings are the same and that non of them is empty

I think this is the longer, tedious and inefficient way to check:


private void checkPasswordSame() {
        String first = password1.getText();
        String second = password2.getText();
        if (first.equals("")) {
            System.out.println("Password can't be empty");

            if ("".equals(second)) {
                System.out.println("Second password is empty");
            }
        } else if (first.equals(second)) {
            System.out.println("Passwords same");
        } else {
            System.out.println("Passwords not the same");
        }

}

Is there a way I can do this in fewer lines?

Upvotes: 0

Views: 124

Answers (4)

Cristian Sulea
Cristian Sulea

Reputation: 264

Efficiently doesn't means fewer lines of code. Are you sure you want a method with fewer lines of code? Or you want a faster method? Below you have a faster method.

    private void checkPasswordSame() {

      final String first = password1.getText();
      final String second = password2.getText();

      final boolean firstIsEmpty = first.isEmpty();
      final boolean secondIsEmpty = second.isEmpty();

      if (firstIsEmpty) {
        System.out.println("Password can't be empty");
      }

      if (secondIsEmpty) {
        System.out.println("Second password is empty");
      }

      if (!firstIsEmpty && !secondIsEmpty) {

        if (first.equals(second)) {
          System.out.println("Passwords same");
        } else {
          System.out.println("Passwords not the same");
        }
      }
    }

Notes:

  1. using #isEmpty() method is faster
  2. you might want to see if second password is empty even when first one is empty (that's why i didn't included second if in the first one)

Upvotes: 0

elevenights
elevenights

Reputation: 138

Can you switch the check way: 1.is match. 2.not empty.

if (first.equals(second)) 
{
    //check one is enough
    if(first == null || first.isEmpty())
    {
        System.out.println("Password can't be empty");
    }
    else
    {
        System.out.println("Passwords same");
    }
} 
else
{
    System.out.println("Passwords not the same");
}

Upvotes: 0

Jean-Karim Bockstael
Jean-Karim Bockstael

Reputation: 1405

If you don't care about which field is empty as both must be filled, you can simplify the emptiness check a bit:

private void checkPasswordSame() {
    String first = password1.getText();
    String second = password2.getText();
    if (first.equals("") || second.equals("")) {
        System.out.println("Both password can't be empty");
    } else if (first.equals(second)) {
        System.out.println("Passwords same");
    } else {
        System.out.println("Passwords not the same");
    }
}

Try not to focus on code length, this is programming not golf; focus on code readability instead. What you're doing should always be obvious to another reader, if it isn't at least provide a comment to explain the tricky part.

As a matter of style, I prefer checking for errors first before processing the normal case, but it's up to you:

private void checkPasswordSame() {
    String first = password1.getText();
    String second = password2.getText();
    if (first.equals("") || second.equals("")) {
        System.out.println("Both password can't be empty");
    } else if (!first.equals(second)) {
        System.out.println("Passwords not the same");
    }
    else {
        System.out.println("Passwords same");
    }
}

Upvotes: 2

piet.t
piet.t

Reputation: 11911

You cloud leave out these lines:

        if ("".equals(second)) {
            System.out.println("Second password is empty");
        }

If the first password isn't empty but the second is the user will get "Passwords not the same" - which I think is a true and sufficient message in this case.

Upvotes: 0

Related Questions