代码之家  ›  专栏  ›  技术社区  ›  Program-Me-Rev

如何有效地检查两个字符串是否相同,并且其中没有一个字符串是空的

  •  0
  • Program-Me-Rev  · 技术社区  · 11 年前

    我认为这是一种耗时、乏味且效率低下的检查方法:

    • 密码字段不为空
    • 两个密码都匹配
    • 两个密码不相同

    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");
            }
    
    }
    

    有没有一种方法可以让我少排几行?

    4 回复  |  直到 11 年前
        1
  •  2
  •   Jean-Karim Bockstael    11 年前

    如果您不关心哪个字段是空的,因为这两个字段都必须填充,那么可以稍微简化空性检查:

    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");
        }
    }
    

    尽量不要关注代码长度,这是编程而不是高尔夫;而是关注代码的可读性。你正在做的事情应该总是对另一个读者显而易见的,如果它不是至少提供一个评论来解释棘手的部分。

    作为一种风格,我喜欢在处理正常情况之前先检查错误,但这取决于您:

    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");
        }
    }
    
        2
  •  0
  •   piet.t Charis A.    11 年前

    你可以省略以下几行:

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

    如果第一个密码不是空的,但第二个密码是空的,则用户将得到“密码不一样”——我认为在这种情况下,这是一个真实而充分的消息。

        3
  •  0
  •   elevenights    11 年前

    你能换一下检查方式吗 1.匹配。 2.不为空。

    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");
    }
    
        4
  •  0
  •   Cristian Sulea    11 年前

    高效并不意味着代码行更少。 是否确实要使用代码行更少的方法?或者你想要更快的方法? 下面是一个更快的方法。

        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");
            }
          }
        }
    

    笔记:

    1. 使用 #isEmpty() 方法更快
    2. 您可能想看看第二个密码是否为空,即使第一个密码为空(这就是为什么我没有包括第二个 如果 在第一个)