Source:
public String toTextTable() { StringBuilder buffer = new StringBuilder(); int numCols = this.getColumnCount(); int numRows = this.getRowCount(); List<String> formatterList = new ArrayList<>(); List<ArrayList<String>> allTableRows = new ArrayList<>(); ArrayList<String> currTableRow = new ArrayList<>(); int longestWordSize = 10; for (int i = 0; i < 2 * numCols + 1; i++) { if (i % 2 == 0) { formatterList.add("%-2s "); currTableRow.add("|"); } else { longestWordSize = Math.max(this.getColumnName(i/2).length(), longestWordSize); formatterList.add("%-10s "); currTableRow.add(this.getColumnName(i/2)); } } allTableRows.add(currTableRow); formatterList.set(formatterList.size()-1, "%-2s%n"); for (int row = 0; row < numRows; row++) { currTableRow = new ArrayList<>(); for (int i = 0; i < numCols * 2 + 1; i++) { if (i % 2 == 0) { currTableRow.add("|"); } else { longestWordSize = Math.max(this.getValueAt(row, i/2).toString().length(), longestWordSize); currTableRow.add(this.getValueAt(row, i/2).toString()); } } allTableRows.add(currTableRow); } if (longestWordSize > 10) { for (int i = 1; i < formatterList.size(); i += 2) { formatterList.set(i, "%-" + longestWordSize + "s "); } } String formatterString = ""; for (int i = 0; i < formatterList.size(); i ++) { formatterString += formatterList.get(i); } for (int i = 0; i < allTableRows.size(); i ++) { if (i == 1) { buffer.append("|-----\n"); } buffer.append(String.format(formatterString, (allTableRows.get(i)).toArray())); } return buffer.toString(); }

Target:
public String toTextTable() { StringBuilder buffer = new StringBuilder(); int numCols = this.getColumnCount(); int numRows = this.getRowCount(); List<String> formatterList = new ArrayList<>(); List<ArrayList<String>> allTableRows = new ArrayList<>(); ArrayList<String> currTableRow = new ArrayList<>(); int longestWordSize = 10; for (int i = 0; i < 2 * numCols + 1; i++) { if (i % 2 == 0) { formatterList.add("%-2s "); currTableRow.add("|"); } else { longestWordSize = Math.max(this.getColumnName(i/2).length(), longestWordSize); formatterList.add("%-10s "); currTableRow.add(this.getColumnName(i/2)); } } allTableRows.add(currTableRow); formatterList.set(formatterList.size()-1, "%-2s%n"); for (int row = 0; row < numRows; row++) { currTableRow = new ArrayList<>(); for (int i = 0; i < numCols * 2 + 1; i++) { if (i % 2 == 0) { currTableRow.add("|"); } else { longestWordSize = Math.max(this.getValueAt(row, i/2).toString().length(), longestWordSize); currTableRow.add(this.getValueAt(row, i/2).toString()); } } allTableRows.add(currTableRow); } if (longestWordSize > 10) { for (int i = 1; i < formatterList.size(); i += 2) { formatterList.set(i, "%-" + longestWordSize + "s "); } } String formatterString = ""; for (int i = 0; i < formatterList.size(); i ++) { formatterString += formatterList.get(i); } int lenColSep = 0; for (int i = 0; i < allTableRows.size(); i ++) { if (i == 1) { char[] colSepArr = new char[lenColSep-4]; Arrays.fill(colSepArr, '-'); String colSep = new String(colSepArr); buffer.append("|"); buffer.append(colSep); buffer.append("|\n"); } String appendStr = String.format(formatterString, (allTableRows.get(i)).toArray()); buffer.append(appendStr); if (i == 0) { lenColSep = appendStr.length(); } } return buffer.toString(); }