[英]Exiting While Loop After User Enters Value in String (Java)
各位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;
}
解決此問題的最佳方法是什么? 感謝您的幫助!
使用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;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.