Linux-Fan

TextTable.java

Oct 20th, 2013
58
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package ma.tools.io;
  2.  
  3. /**
  4.  * @version 1.1
  5.  */
  6. public class TextTable {
  7.    
  8.     private String[][] data;
  9.     private int[] cell_max;
  10.    
  11.     public TextTable(Object[][] data) {
  12.         cell_max = new int[data[0].length];
  13.        
  14.         for(int j = 0; j < data[0].length; j++) {
  15.             int longest = 0;
  16.             for(int i = 0; i < data.length; i++) {
  17.                 int len = data[i][j].toString().length();
  18.                 if(len > longest) {
  19.                     longest = len;
  20.                 }
  21.             }
  22.             cell_max[j] = longest;
  23.         }
  24.        
  25.         this.data = new String[data.length][data[0].length];
  26.        
  27.         for(int i = 0; i < data[0].length; i++) {
  28.             for(int j = 0; j < data.length; j++) {
  29.                 this.data[j][i] = createTableEntry(cell_max[i], data[j][i].toString());
  30.             }
  31.         }
  32.     }
  33.    
  34.     private String createTableEntry(int length, String entry) {
  35.         int entryLength = entry.length();
  36.         StringBuffer entry_buf = new StringBuffer(" " + entry);
  37.         for(int i = 0; i < (length - entryLength); i++) {
  38.             entry_buf.append(" ");
  39.         }
  40.         entry_buf.append(" ");
  41.         return entry_buf.toString();
  42.     }
  43.    
  44.     public String toString() {
  45.         StringBuffer buf = new StringBuffer();
  46.         for(int i = 0; i < data.length; i++) {
  47.             for(int j = 0; j < cell_max.length; j++) {
  48.                 buf.append(data[i][j]);
  49.             }
  50.             buf.append("\n");
  51.         }
  52.         return buf.toString();
  53.     }
  54. }
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×