繁体   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