您好,StackOverflowers,我希望您的所有日子过得很好。
我对Java编程比较陌生,并且发现自己有点咸菜。
我想做的是
不幸的是,下面的while循环在第一个实例之后退出,并且在下面的代码示例中不再继续;
public String getAccountName() {
this.accountName = JOptionPane.showInputDialog(null, "Please enter a nick name for your new account (e.g. Savings Account)");
if (this.accountName!= null) {
while (this.accountName != null) {
this.accountName = JOptionPane.showInputDialog(null, "Error! Please enter a valid name for your new account");
if (this.accountName.contains("")){return this.accountName;
}
}
}
return this.accountName;
}
解决此问题的最佳方法是什么? 感谢您的帮助!
答案 0 :(得分:0)
使用StringUtils.isBlank方法(https://commons.apache.org/proper/commons-lang/apidocs/org/apache/commons/lang3/StringUtils.html)检查accountName值:
this.accountName = JOptionPane.showInputDialog(null, "Please enter a nick name for your new account (e.g. Savings Account)");
while (StringUtils.isBlank(this.accountName)) {
this.accountName = JOptionPane.showInputDialog(null, "Error! Please enter a valid name for your new account");
}
return this.accountName;