[英]A null check on two entity fields
I am working on a JAVA EE application.我正在开发一个 JAVA EE 应用程序。 I am creating a form using itext.
我正在使用 itext 创建一个表单。 This would be saved as a pdf file.
这将被保存为 pdf 文件。 I have two fields (ID Number and Passport Number).
我有两个字段(身份证号码和护照号码)。 I am currently struggling with performing a null check on them.
我目前正在努力对它们进行空检查。 For instance, if the ID number field is null, then check whether or not the Passport Number field is null.
例如,如果身份证号码字段为空,则检查护照号码字段是否为空。 If both are null, then display a text "No ID Numbers detected."
如果两者都为空,则显示文本“未检测到 ID 号”。
ID Number and passport numbers are of type string.身份证号码和护照号码是字符串类型。
This is my code presently.这是我目前的代码。
PdfPCell a3 = new PdfPCell();
a3.setColspan(4);
a3.addElement(PdfUtil.getSmallParagraph("ID NUMBER: " + (client.getIdNumber() != null ? client.getPassportNo() : "")));
tbl.addCell(a3);
You can try something like this你可以试试这样的
if(client.getIdNumber() == null && client.getPassportNumber() == null)
{
//print No ID Numbers detected
}
if(client.getIdNumber()!= null)
{
if(client.getPassportNumber()!= null)
{
// do something
}
}
In a single line, with a nested, nasty ternary operator :在一行中,带有嵌套的、讨厌的三元运算符:
String text = "ID NUMBER: " + (client.getIdNumber() == null ? (client.getPassportNo() != null ? client.getPassportNo() : "NO ID FOUND") : client.getIdNumber());
Absolutely not the best for code readability ...绝对不是最好的代码可读性......
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.