Java Matrix to String formatTable(String[][] table)

Here you can find the source of formatTable(String[][] table)

Description

Formats the given array of arrays of String s

License

Open Source License

Parameter

Parameter Description
table the given array of arrays of String s

Return

the formated String

Declaration

public static String formatTable(String[][] table) 

Method Source Code

//package com.java2s;
/******************************************************************************
 * Copyright (c) 2007 Stefan Franke, Robert Hanussek, Benjamin Keil,          *
 *                    Steffen Kie?, Johannes Langauf,                         *
 *                    Christoph Marian M?ller, Igor Podolskiy,                *
 *                    Tilmann Scheller, Michael Starzmann, Markus Wittlinger  *
 * All rights reserved. This file may be used, modifies and redistributed     *
 * under the terms of either the Eclipse Public License v1.0 which            *
 * accompanies this distribution and is available at                          *
 * http://www.eclipse.org/legal/epl-v10.html or the MIT license, available at *
 * http://www.opensource.org/licenses/mit-license.php                         *
 ******************************************************************************/

import java.util.ArrayList;

import java.util.List;

public class Main {
    /**//  ww w  .  j  a  v  a 2 s . c  o  m
     * Formats the given array of arrays of {@link String}s
     * 
     * @param table
     *            the given array of arrays of {@link String}s
     * @return the formated String
     */
    public static String formatTable(String[][] table) {
        if (table == null) {
            throw new NullPointerException("table == null");
        }

        final List<List<String>> list = new ArrayList<List<String>>();

        for (final String[] a : table) {
            final List<String> list2 = new ArrayList<String>();
            for (final String s : a) {
                list2.add(s);
            }
            list.add(list2);
        }

        return formatTable(list);
    }

    /**
     * Formats the given {@link List} of {@link List} of {@link String}s
     * 
     * @param table
     *            the given {@link List} of {@link List} of {@link String}s
     * @return the formated String
     */
    public static String formatTable(List<List<String>> table) {
        if (table == null) {
            throw new NullPointerException("table == null");
        }

        final StringBuilder sb = new StringBuilder();

        int columns = 0;
        final List<Integer> columnSizes = new ArrayList<Integer>();

        for (List<String> row : table) {
            if (row == null) {
                throw new NullPointerException("row == null");
            }
            final int rowLength = row.size();
            if (rowLength > columns) {
                columns = rowLength;
            }
            int i = 0;
            for (String cell : row) {
                if (cell == null) {
                    throw new NullPointerException("cell == null");
                }
                int columnSize = 0;
                if (columnSizes.size() > i) {
                    columnSize = columnSizes.get(i);
                } else {
                    columnSizes.add(0);
                }
                if (cell.length() > columnSize) {
                    columnSize = cell.length();
                }
                columnSizes.set(i, columnSize);
                i++;
            }
        }

        for (List<String> row : table) {
            int pos = 0;
            int i = 0;
            for (String cell : row) {
                if (i == columns - 1) {
                    sb.append(wrapLines(cell, pos));
                } else {
                    sb.append(cell);
                    final int columnSize = columnSizes.get(i);
                    for (int j = cell.length(); j < columnSize; j++) {
                        sb.append(" ");
                    }
                    pos += columnSize;
                }
                i++;
            }
            sb.append(System.getProperty("line.separator"));
        }

        return sb.toString();
    }

    /**
     * Wrap lines, replace \n by newline
     * 
     * @param string
     *            the {@link String} to wrap
     * @param spacesBefore
     *            the amount of whitespaces before the string
     * @return the wrapped {@link String}
     */
    public static String wrapLines(String string, int spacesBefore) {
        final int screenLength = 80; // TODO: don't hardcode this, try stuff like $COLUMNS

        final int length = screenLength - spacesBefore;

        if (string == null) {
            throw new NullPointerException("str == null");
        }

        if (length < 20) {
            // We have less than 20 chars left. give up.
            return string;
        }

        final String[] lines = string.split("\n", -1);

        final StringBuilder sb = new StringBuilder();

        for (int i = 0; i < lines.length; i++) {
            final String line = lines[i];

            int linePos = 0;

            while (linePos < line.length()) {
                if (length >= line.length() - linePos) {
                    // string will fit
                    sb.append(line.substring(linePos));
                    linePos = line.length();
                } else {
                    int whiteSpacePos = linePos + length;
                    while (whiteSpacePos >= linePos
                            && !Character.isWhitespace(line
                                    .charAt(whiteSpacePos))) {
                        whiteSpacePos--;
                    }
                    if (whiteSpacePos >= linePos) {
                        final int printedChars = whiteSpacePos - linePos
                                + 1;
                        while (whiteSpacePos > linePos
                                && Character.isWhitespace(line
                                        .charAt(whiteSpacePos - 1))) {
                            whiteSpacePos--;
                        }
                        sb.append(line.substring(linePos, whiteSpacePos));
                        sb.append(System.getProperty("line.separator"));
                        for (int j = 0; j < spacesBefore; j++) {
                            sb.append(" ");
                        }
                        linePos += printedChars;
                    } else {
                        // no white space found, give up
                        sb.append(line.substring(linePos));
                    }
                }
            }

            if (i != lines.length - 1) {
                sb.append(System.getProperty("line.separator"));
                for (int j = 0; j < spacesBefore; j++) {
                    sb.append(" ");
                }
            }
        }

        return sb.toString();
    }
}

Related

  1. Array2DToString(double[][] array)
  2. array2DToString(int[][] grid)
  3. matrix2string(int[][] matrix)
  4. matrix_toString(int[][] matrix)
  5. matrixToString(double[][] matrix, int digit, String[] names)
  6. matrixToString(float[][][] matrix)