2014-09-05 76 views
0

我觉得这是检查的时间越长,繁琐和低效的方式:如何有效检查两个字符串是相同的,它们的非空

  • 该密码字段不为空
  • 那这两个密码相匹配
  • 两个密码是不一样的

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

} 

有没有办法以较少的行数来做到这一点?

+0

为什么所有的等于(“”)在这个问题和答案? isEmpty()方法有什么问题?有没有我不知道的一些魔术迷你优化技巧? – Gimby 2014-09-05 07:58:25

回答

2

如果你不关心哪个字段为空既是必须填写,可以简化空虚查了一下:

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

你离开云这些行:

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

如果第一个密码不为空,但第二个是用户将得到“密码不一样” - 我认为这是一个真正的和足够的信息在这种情况下。

0

可以切换检查方式: 1.is比赛。 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"); 
} 
+0

第4行的if(first == null || ...)没用,如果first == null,你会得到第1行的NPE。 – blafasel 2014-09-05 09:00:36

+0

yes,编辑时出错,谢谢 – elevenights 2014-09-05 09:04:27

0

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

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. 你可能想看看第二密码是空的,即使第一个是空的(这就是为什么我没有” t包括第二个如果在第一个)
相关问题