【发布时间】: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