Want more features on Pastebin? Sign Up, it's FREE!
Guest

Simple Column Output Formatter

By: a guest on Nov 14th, 2011  |  syntax: Java  |  size: 2.57 KB  |  views: 28  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. public class ColumnPrintStringBuffer {
  2.         //we cannot extend from Stringbuffer (it's final), so we aggregate it
  3.         private StringBuffer sb;
  4.         private int columnLength;
  5.         private int currentLineLength = 0;
  6.         public ColumnPrintStringBuffer(int columnLength) {
  7.             sb = new StringBuffer();
  8.             this.columnLength = columnLength;
  9.         }
  10.  
  11.         public ColumnPrintStringBuffer append(String str) {
  12.             StringTokenizer lineTokenizer = new StringTokenizer(str, "\n\r\f");
  13.             while (lineTokenizer.hasMoreTokens()) {
  14.                 appendLine(lineTokenizer.nextToken());
  15.                 if(lineTokenizer.hasMoreTokens())
  16.                     sb.append("\n");
  17.             }
  18.             return this;
  19.         }
  20.  
  21.         protected void appendLine(String line){
  22.             StringTokenizer wordTokenizer = new StringTokenizer(line, " \t");
  23.             if(wordTokenizer.hasMoreTokens()) {
  24.                 String current = wordTokenizer.nextToken();
  25.                 String lookAhead = null;
  26.                 while(wordTokenizer.hasMoreTokens()) {
  27.                     lookAhead = wordTokenizer.nextToken();
  28.                     appendWord(current, lookAhead);
  29.                     current = lookAhead;
  30.                     lookAhead = null;
  31.                 }
  32.                 appendWord(current, lookAhead);
  33.             }
  34.         }
  35.  
  36.         protected void appendWord(String word, String lookAheadWord) {
  37.             if(currentLineLength + word.length() <= columnLength){
  38.                 currentLineLength += word.length();
  39.                 sb.append(word);
  40.                 if(lookAheadWord != null && currentLineLength+ 1 + lookAheadWord.length() <= columnLength) {
  41.                     currentLineLength++;
  42.                     sb.append(" ");
  43.                 } else {
  44.                     currentLineLength = 0;
  45.                     if(lookAheadWord != null)
  46.                         sb.append("\n");
  47.                 }
  48.             } else {
  49.                 currentLineLength = word.length();
  50.                 sb.append("\n").append(word);
  51.             }
  52.         }
  53.  
  54.         @Override
  55.         public String toString() {
  56.             return sb.toString();
  57.         }
  58.  
  59.         public static void main (String[] args) {
  60.             String s1 = "The quick brown fox jumped over the lazy dog.\n";
  61.             String s2 = "The quick\nbrown fox jumped over\nthe lazy dog.";
  62.             ColumnPrintStringBuffer cpsb = new ColumnPrintStringBuffer(10);
  63.             cpsb.append(s1);
  64.             cpsb.append(s2);
  65.             System.out.println(cpsb.toString());
  66. }
  67.  
clone this paste RAW Paste Data