【问题标题】:How to efficiently check if two Strings are the same and that non of them is empty如何有效地检查两个字符串是否相同并且它们都不是空的
【发布时间】:2014-09-05 07:25:12
【问题描述】:

我认为这是一种冗长、乏味且低效的检查方式:

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

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

}

有没有一种方法可以用更少的行数做到这一点?

【问题讨论】:

  • 为什么这个问题和答案中的所有 equals("")? isEmpty() 方法有什么问题?是否有一些我不知道的神奇迷你优化技巧?

标签: java string passwords textfield


【解决方案1】:

如果你不关心哪个字段是空的,因为两者都必须填写,你可以稍微简化一下空位检查:

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

    你忽略了这些行:

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

    如果第一个密码不为空,但第二个密码为空,用户将收到“密码不相同”——我认为在这种情况下这是一个真实且充分的信息。

    【讨论】:

      【解决方案3】:

      是否可以切换检查方式: 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 行的 if(first == null || ... 没用。如果first == null 你会在第 1 行得到一个 NPE。
      【解决方案4】:

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

          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. 您可能想查看第二个密码是否为空,即使第一个密码为空(这就是为什么我没有在第一个密码中包含第二个 if

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-05-25
        • 1970-01-01
        • 2020-04-28
        • 2016-05-04
        • 1970-01-01
        相关资源
        最近更新 更多