簡體   English   中英

while循環jtextfield僅接受字母

[英]while loop jtextfield accepts only letters

我想強制文本字段僅接受字母和空格。 我嘗試了這個對我來說看起來不錯的函數,但僅輸出:

i = 0,所以我可以看到這是局部變量或類似問題。

public void ff()
{Boolean bool=true;
int i=0;


     name=textField.getText();
     while(bool && i<name.length())
     {
         if(Character.isLetter(name.charAt(i)))
             i++;
         else
            bool=false;
     }
    if (bool=true) 
            System.out.println("accepted"+i);
    else 
            System.out.println("wrong"+i);

}

你能幫我弄清楚嗎? 並且,請不要使用documentfilter或formattedtext。

bool=true是分配,不是比較。

if (bool=true)更改為if (bool==true)if (bool) 否則,您將始終打印accepted

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

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