-3

Why does setText() show "N/A" when Info2 = ""? Why does data appear when Info2 has data but when Info2 does not have data my setText() call shows blank, not "N/A"?

JSON CODE >> {"AirlineInfoResult":{"name":"Malaysian Airlines System","shortname":""}}

String Info2 = ... do something util receive result from shortname

if(Info2.equals("")) {
    shortname.setText("N/A");
}
else if(!Info2.equals("")) {
    shortname.setText(Info2);
}
takendarkk
  • 3,347
  • 8
  • 25
  • 37
Flyzy Dev
  • 91
  • 1
  • 3
  • 10

2 Answers2

1
shortname.setText((Info2 == null) || Info2.isEmpty()) ? "N/A" : Info2);

or,

shortname.setText("".equals(Info2) ? "N/A" : Info2);
d.moncada
  • 16,900
  • 5
  • 53
  • 82
0

Just do:

if(Info2.equals(""))
{
    shortname.setText("N/A");
}
else
{
    shortname.setText(Info2);
}

By default else is ran if your condition is false. Also keep in mind if Info2 can be null then you should add:

if("".equals(Info2))
{
    shortname.setText("N/A");
}
else
{
    shortname.setText(Info2);
}

The phrase "".equals(Info2) is called a yoda expression and returns false if Info2 is null.

Anubian Noob
  • 13,426
  • 6
  • 53
  • 75