簡體   English   中英

用戶在字符串中輸入值后退出While循環(Java)

[英]Exiting While Loop After User Enters Value in String (Java)

各位StackOverflowers,您好,我希望您一切順利。

我對Java編程比較陌生,並且發現自己有點咸菜。

我想做的是

  1. Java中的輸入驗證-我想確保JOptionPane.showInput窗格繼續(使用while循環)重新出現,直到用戶輸入了在“ this.accountName”字符串中捕獲的值為止;並且
  2. 從那里,一旦用戶在JOptionPane.showInput窗格中輸入了內容,我想退出循環並繼續執行OO程序中擁有的其他方法。

不幸的是,下面的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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM