当前位置: 动力学知识库 > 问答 > 编程问答 >

java - 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:

  • that password fields are not empty
  • that both passwords match
  • that both passwords are not the same


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?

网友答案:

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");
    }
}
网友答案:

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.

网友答案:

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");
}
网友答案:

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)
分享给朋友:
您可能感兴趣的文章:
随机阅读: