在 java 中使用字符串格式打印列
Printing column using String Format in java
我尝试以 java 的形式打印输出结果
---------------------------------
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
---------------------------------
到目前为止,这是我的代码
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");
并且该代码的输出是
---------------------------------
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
---------------------------------
有谁知道如何修改我的代码以便我可以按我想要的方式打印:)
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");
因为 Pineapple Juice 的长度比其他的长,做了 15s 的空格,希望这对你有帮助
以下代码将漂亮地打印您的数据。但是,您将无法正确设置浮动格式...
/**
* 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));
}
测试代码:
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");
输出:
------------------------------
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
------------------------------
我尝试以 java 的形式打印输出结果
---------------------------------
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
---------------------------------
到目前为止,这是我的代码
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");
并且该代码的输出是
---------------------------------
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
---------------------------------
有谁知道如何修改我的代码以便我可以按我想要的方式打印:)
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");
因为 Pineapple Juice 的长度比其他的长,做了 15s 的空格,希望这对你有帮助
以下代码将漂亮地打印您的数据。但是,您将无法正确设置浮动格式...
/**
* 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));
}
测试代码:
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");
输出:
------------------------------
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
------------------------------