简体   繁体   中英

Printing column using String Format in java

I try to print output result in java something like this

---------------------------------
             qty   price   code
Coffee       2     12.000    T1
Tea          1     10.000    T1
Orange Juice 1     15.000    T1
Pineapple    1     20.000    T1
Juice
Tap Water    1     9000      T1
---------------------------------

This is my code so far

    System.out.print("\n");
    System.out.print("--------------------------------");
    System.out.print("\n");
    System.out.print(String.format("%-12s %-5s %-7s %-4s\n","","qty","price","code"));
    System.out.print("-------------------------------\n");
    System.out.print(String.format("%-12s %-5s %-7s %4s\n","Coffee","2","12.000","T1"));
    System.out.print(String.format("%-12s %-5s %-7s %4s\n","Tea","1","10.000","T1"));
    System.out.print(String.format("%-12s %-5s %-7s %4s\n","Orange Juice","1","15.000","T1"));
    System.out.print(String.format("%-12s %-5s %-7s %4s\n","Pineapple Juice","1","20.000","T1"));
    System.out.print(String.format("%-12s %-5s %-7s %4s\n","Tap Water","1","9000","T1"));
    System.out.print("--------------------------------");
    System.out.print("\n");

and the output from that code is

---------------------------------
             qty   price   code
Coffee       2     12.000    T1
Tea          1     10.000    T1
Orange Juice 1     15.000    T1
Pineapple Juice 1     20.000    T1
Tap Water    1     9000      T1
---------------------------------

Does anyone know how to modify my code so i can print like i want :)

The following code will pretty print your data. However, you will lose the ability to properly format your floats...

/**
 * Pretty prints a given array of {@link Objects} in a grid. The resolution
 * of the grid depends on the amount of {@link Object}s passed along.
 * 
 * @param columns
 *            the number of columns present in the data.
 * @param spacing
 *            the number of spaces between each column.
 * @param objects
 *            the objects which need to be printed.
 * @throws NullPointerException
 *             when the given array of {@link Object}s is null.
 * @throws IllegalArgumentException
 *             when there are not enough objects to fill the given amount of
 *             columns. (i.e. objects.length must be divisible by columns).
 * @throws IllegalArgumentException
 *             when the amount of spacing is smaller than zero.
 */
public void prettyPrint(int columns, int spacing, Object... objects)
        throws NullPointerException, IllegalArgumentException {
    if (objects == null)
        throw new NullPointerException(
                "the given array of objects is null!");
    if (objects.length % columns != 0)
        throw new IllegalArgumentException(
                "not enough objects are passed along to fill the columns!");
    if (spacing < 0)
        throw new IllegalArgumentException(
                "the amount of spacing should be larger than zero!");
    int rows = objects.length / columns;

    // convert all objects to strings
    String[] strings = new String[objects.length];
    for (int i = 0; i < objects.length; ++i)
        strings[i] = objects[i].toString();

    // determine the maximum length of the items in each column
    int totalLength = 0;
    int[] maxLength = new int[columns];
    for (int column = 0; column < columns; ++column) {
        int maximum = 0;
        for (int row = 0; row < rows; ++row)
            maximum = Math.max(maximum,
                    strings[column + row * columns].length());
        maxLength[column] = maximum;
        totalLength += maximum;
    }

    // determine the total width of the output string (this is the sum of
    // maximum lengths of each column + the length caused by adding
    // (columns-1) spaces between the columns.
    totalLength += (columns - 1) * spacing;

    // print the header
    System.out.println(repeat("-", totalLength));

    for (int row = 0; row < rows; ++row) {
        for (int column = 0; column < columns; ++column) {
            // print the string
            String string = strings[column + row * columns];
            System.out.print(string);

            // print the spaces except after the element in the last column
            if (column < columns - 1) {
                int spaces = maxLength[column] - string.length() + spacing;
                System.out.print(repeat(" ", spaces));
            }
        }
        System.out.println();
    }
    // print the footer
    System.out.println(repeat("-", totalLength));
}

/**
 * Repeats the given {@link String} n times.
 * 
 * @param str
 *            the {@link String} to repeat.
 * @param n
 *            the repetition count.
 * @throws IllegalArgumentException
 *             when the given repetition count is smaller than zero.
 * @return the given {@link String} repeated n times.
 */
public static String repeat(String str, int n) {
    if (n < 0)
        throw new IllegalArgumentException(
                "the given repetition count is smaller than zero!");
    else if (n == 0)
        return "";
    else if (n == 1)
        return str;
    else if (n % 2 == 0) {
        String s = repeat(str, n / 2);
        return s.concat(s);
    } else
        return str.concat(repeat(str, n - 1));
}

/**
 * Repeats the given {@link String} n times.
 * 
 * @param str
 *            the {@link String} to repeat.
 * @param n
 *            the repetition count.
 * @throws IllegalArgumentException
 *             when the given repetition count is smaller than zero.
 * @return the given {@link String} repeated n times.
 */
public static String repeat(String str, int n) {
    if (n < 0)
        throw new IllegalArgumentException(
                "the given repetition count is smaller than zero!");
    else if (n == 0)
        return "";
    else if (n == 1)
        return str;
    else if (n % 2 == 0) {
        String s = repeat(str, n / 2);
        return s.concat(s);
    } else
        return str.concat(repeat(str, n - 1));
}

Test code:

prettyPrint(4, 2, "", "qty", "price", "code", "Coffee", 2, 12.000,
            "T1", "Tea", 1, 10.000, "T1", "Orange Juice", 1, 15.000, "T1",
            "Juice", "", "", "", "Tap Water", 1, 9000, "T1");

Output:

------------------------------
              qty  price  code  
Coffee        2    12.0   T1    
Tea           1    10.0   T1    
Orange Juice  1    15.0   T1    
Juice                           
Tap Water     1    9000   T1    
------------------------------
System.out.print("\n");
    System.out.print("--------------------------------");
    System.out.print("\n");
    System.out.print(String.format("%-15s %-5s %-7s %-4s\n","","qty","price","code"));
    System.out.print("-------------------------------\n");
    System.out.print(String.format("%-15s %-5s %-7s %4s\n","Coffee","2","12.000","T1"));
    System.out.print(String.format("%-15s %-5s %-7s %4s\n","Tea","1","10.000","T1"));
    System.out.print(String.format("%-15s %-5s %-7s %4s\n","Orange Juice","1","15.000","T1"));
    System.out.print(String.format("%-15s %-5s %-7s %4s\n","Pineapple Juice","1","20.000","T1"));
    System.out.print(String.format("%-15s %-5s %-7s %4s\n","Tap Water","1","9000","T1"));
    System.out.print("--------------------------------");
    System.out.print("\n");

made 15s spaces because Pineapple Juice length is more than others, hope this helps

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM