如何检查 Java 中的整数文本字段是否为空?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23669004/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to check if an Integer text field is empty in Java?
提问by user3541302
I have a GUI which stores the Bank Account number. During validation, I need to check if the user has entered the account number... For string fields, isEmpty() method can be used... but for integer fields like bank acc no, Integer.parseInt(jTextField1.getText()).isEmpty() would give an error. How to check for null fields when it is an integer?
我有一个存储银行帐号的 GUI。在验证过程中,我需要检查用户是否输入了帐号...对于字符串字段,可以使用 isEmpty() 方法...但是对于像 bank acc no, Integer.parseInt(jTextField1.getText() 这样的整数字段).isEmpty() 会报错。当它是一个整数时如何检查空字段?
回答by Nambi
Check is Empty before parsing it into integer like
在将其解析为整数之前检查为空
if(!jTextField1.getText().isEmpty()){
Integer.parseInt(jTextField1.getText());
}
回答by Md Johirul Islam
You should rather try the following code
你应该试试下面的代码
if(!(jTextField1.getText().isEmpty()))
{
int accountNumber=Integer.parseInt(jTextField1.getText());
}