Either use .append all the time (rather than the + concatenation - since that does new StringBuffers and appends itself) or use String.format("%%nBatch #: %d | %s | %s | %s", i+1, title, id);  // or something like that, having checked for nulls earlier