This is not a true answer, because I will not use the ternary operator here.
If you need to combine lines that delete empty ones, you can write a general function (without error checking, without optimization, take it as an example):
public static String join(String[] array, char separator) { StringBuffer result = new StringBuffer(); for (int i = 0; i < array.length; ++i) { if (array[i] != null && array[i].length() != 0) { if (result.length() > 0) result.append(separator); result.append(array[i]); } } return result.toString(); }
This is a rather long version than the "built-in" version, but it works regardless of the number of lines you want to join (and you can change it to use a variable number of parameters). This will make the code in which you will use it much more clearly than any if
tree.
Something like that:
public static String join(char separator, String... items, ) { StringBuffer result = new StringBuffer(); for (String item: items) { if (item != null && item.length() != 0) { if (result.length() > 0) result.append(separator); result.append(item); } } return result.toString(); }
source share