while循环jtextfield只接受字母

时间:2014-08-13 14:51:51

标签: java while-loop jtextfield

我想强制文本字段只接受字母和空格。 我试过这个看起来不错的功能,但它只打印出来:

接受,i = 0所以我可以看到它是局部变量或类似smthg的问题;

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。

1 个答案:

答案 0 :(得分:0)

bool=true是作业,而不是比较。

if (bool=true)更改为if (bool==true)if (bool)。否则,您将始终打印accepted