简体   繁体   English

while循环jtextfield仅接受字母

[英]while loop jtextfield accepts only letters

I want to force a textfield to only accept letters and white space. 我想强制文本字段仅接受字母和空格。 I tried this function that looks fine to me but it only prints: 我尝试了这个对我来说看起来不错的函数,但仅输出:

accepted and i=0 So I can see that it's a problem of local variable or smthg like that; 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);

}

Can you help me to figure it out? 你能帮我弄清楚吗? and Please I want to use this function not documentfilter or formattedtext. 并且,请不要使用documentfilter或formattedtext。

bool=true is assignment, not comparison. bool=true是分配,不是比较。

Change if (bool=true) to if (bool==true) or if (bool) . if (bool=true)更改为if (bool==true)if (bool) Otherwise, you'll always print accepted . 否则,您将始终打印accepted

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM