java中使用字符串格式打印列
我尝试用java打印输出结果,类似这样java中使用字符串格式打印列,java,string-formatting,Java,String Formatting,我尝试用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 T
---------------------------------
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
---------------------------------
是否有人知道如何修改我的代码,以便我可以按需要打印:)
因为菠萝汁的长度比其他的要长,所以留了15秒的空间,希望这有帮助
因为菠萝汁的长度比其他的要长,所以用了15秒的空格,希望这对你有所帮助。下面的代码可以很好地打印你的数据。但是,您将失去正确设置浮动格式的能力
/**
* 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));
}
输出:
------------------------------
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
------------------------------
下面的代码将很好地打印您的数据。但是,您将失去正确设置浮动格式的能力
/**
* 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));
}
输出:
------------------------------
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
------------------------------