Posted By

borysn on 05/31/13


Tagged

java print pretty line Generic per


Versions (?)

Generic pretty print based on chars per line


 / Published in: Java
 

Generic method for printing a list to the console based on the number of characters per line.

  1. private <T extends Object> String prettyPrintCharsPerLine(T item, int numberOfCharsPerLine) {
  2. // initialize a mutable string
  3. StringBuffer pretty = new StringBuffer(item.toString());
  4.  
  5. // now insert a line break at every point necessary
  6. // the break comes after the numberOfCharsPerLine
  7. int pos = numberOfCharsPerLine;
  8. while (pos < pretty.length()) {
  9. pretty.insert(pos+1, '\n');
  10. pos += numberOfCharsPerLine;
  11. }
  12.  
  13. return pretty.toString();
  14. }

Report this snippet  

You need to login to post a comment.