简体   繁体   English

如何检查TextField是否仅包含字符

[英]How to check if the TextField contains only characters

In the code below, I need to know if the text held by the JTextField contains only characters or if it also contains numeric entries. 在下面的代码中,我需要知道JTextField所保存的文本是否仅包含字符或是否还包含数字条目。

ResultSet rs= ItemQuery.findItem(jTextfield1.getText());
while(rs.next()){
    i_name =rs.getString(2);
    jLabel5.setText(i_name);
    currStock = Integer.toString(rs.getInt(3));
    jLabel6.setText(currStock);
    date = new SimpleDateFormat("yyyy-MM-dd").format(rs.getDate(4));                
    jLabel8.setText(date);
}

You can use a loop to find any digits or any thing else other than characters. 您可以使用循环查找字符以外的任何数字或其他任何东西。

flag=1;
for (int i=0;i++;i<string.size()){
   if (!Character.isLetter(string.charat(i))){
        flag=0;
        break;
   }
}

 if (flag==0)
  // control enters this if statement when any thing other than letters is encountered in the string

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

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