-3

I am working on a JAVA EE application. I am creating a form using itext. This would be saved as a pdf file. 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 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);
akin
  • 1
  • 6

2 Answers2

1

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 
  }
}
RockAndRoll
  • 2,247
  • 2
  • 16
  • 35
1

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 ...

aleroot
  • 71,077
  • 30
  • 176
  • 213