I'm trying to conver a java Object to a string separated by "SEPARATOR".
Example
public class Person{
int id;
String name;
String age;
String sex;
}
Main method
Person per = new Person(1, "Kevin", "20", "Male");
String objectAsText = per.ConvertObjectToString(per, "|");
Expected output
1|Kevin|20|Male
Note that the java object is not a list. And I also want to add a validation so the method ConvertObjectToString can avoid printing object values that are null. ie
Person per = new Person(1, "Kevin", "Male");
String objectAsText = per.ConvertObjectToString(per, "|");
Expected output
1|Kevin|Male
I know that this can be achieved using toString method, but I want to know if there is any other efficient way. Also I don't want to check value by value if null since the java object can be large.
toString()? Just make theConvertObjectToStringmethod do what you want. BTW: ifConvertObjectToStringis a member method ofPerson, it shouldn't take thePersonas an argument.ConvertObjectToStringmethod would automatically adapt if more fields were added later? If so, edit your your Question to say so. Your Question is not clear as presently written.