英文:
How can i check the value of JFormattedTextField is what i need?
问题
MaskFormatter mf = new MaskFormatter("(0###) ### - ## - ##"); // 格式
JFormattedTextField formattedTextField = new JFormattedTextField(mf);
使用这段代码,我可以从用户那里获取一个电话号码。例如:"(0444) 111 - 12 - 34"。
如果用户没有输入任何内容,我会得到一个字符串,类似于:"(0 ) - - "。我该如何检查它是否为空,以及用户是否输入了所有的数字。
<details>
<summary>英文:</summary>
MaskFormatter mf = new MaskFormatter("(0###) ### - ## - ##"); // format
JFormattedTextField formattedTextField = new JFormattedTextField(mf);
With this code i get a phone number from user. For example "(0444) 111 - 12 - 34".
If user write nothing i get a string like this "(0 ) - - ".How can i check it if it is empty or not. And did the user write all numbers.
</details>
# 答案1
**得分**: 0
```java
String text = formattedTextField.getText().toString();
Boolean flag1 = Character.isDigit(text.charAt(2));
if(flag1){
System.out.println("NUMBER");
}else{
System.out.println("NOT NUMBER");
}
我可以使用这段代码进行检查。
英文:
String text = formattedTextField.getText().toString();
Boolean flag1 = Character.isDigit(text.charAt(2));
if(flag1){
System.out.println("NUMBER");
}else{
System.out.println("NOT NUMBER");
}
I can check with this code.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论