Java:更容易漂亮的打印?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1875936/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java: Easier pretty printing?
提问by Nick Heiner
At the end of my computations, I print results:
在我的计算结束时,我打印结果:
System.out.println("\nTree\t\tOdds of being by the sought author");
for (ParseTree pt : testTrees) {
conditionalProbs = reg.classify(pt.features());
System.out.printf("%s\t\t%f", pt.toString(), conditionalProbs[1]);
System.out.println();
}
This produces, for instance:
这会产生,例如:
Tree Odds of being by the sought author
K and Burstner 0.000000
how is babby formed answer 0.005170
Mary is in heat 0.999988
Prelim 1.000000
Just putting two \tin there is sort of clumsy - the columns don't really line up. I'd rather have an output like this:
只是把两个\t放在那里有点笨拙 - 列并没有真正对齐。我宁愿有这样的输出:
Tree Odds of being by the sought author
K and Burstner 0.000000
how is babby formed answer 0.005170
Mary is in heat 0.999988
Prelim 1.000000
(note: I'm having trouble making the SO text editor line up those columns perfectly, but hopefully you get the idea.)
(注意:我无法让 SO 文本编辑器完美地排列这些列,但希望您能理解。)
Is there an easy way to do this, or must I write a method to try to figure it out based on the length of the string in the "Tree" column?
有没有一种简单的方法可以做到这一点,或者我必须编写一种方法来尝试根据“树”列中字符串的长度来找出它?
回答by falstro
You're looking for field lengths. Try using this:
您正在寻找字段长度。尝试使用这个:
printf ("%-32s %f\n", pt.toString(), conditionalProbs[1])
The -32 tells you that the string should be left justified, but with a field length of 32 characters (adjust to your liking, I picked 32 as it is a multiple of 8, which is a normal tab stop on a terminal). Using the same on the header, but with %sinstead of %fwill make that one line up nicely too.
-32 告诉您该字符串应该左对齐,但字段长度为 32 个字符(根据您的喜好进行调整,我选择了 32,因为它是 8 的倍数,这是终端上的正常制表位)。在标题上使用相同的内容,但使用%s而不是%f将使其排列得很好。
回答by Carl Smotricz
What you need is the amazing yet free format().
您需要的是惊人而免费的format()。
It works by letting you specify placeholders in a template string; it produces a combination of template and values as output.
它的工作原理是让您在模板字符串中指定占位符;它产生模板和值的组合作为输出。
Example:
例子:
System.out.format("%-25s %9.7f%n", "K and Burstner", 0.055170);
%sis a placeholder for Strings;%25smeans blank-pad any given String to 25 characters.%-25smeans left-justify the String in the field, i.e. pad to the right of the string.%9.7fmeans output a floating-point number with 9 places in all and 7 to the right of the decimal.%nis necessary to "do" a line termination, which is what you're otherwise missing when you go fromSystem.out.println()toSystem.out.format().
%s是字符串的占位符;%25s表示将任何给定的字符串空白填充到 25 个字符。%-25s表示将字段中的字符串左对齐,即填充到字符串的右侧。%9.7f表示输出一个总共9位,小数点右边7位的浮点数。%n要“做”一个线路终端,这是你当你走的时候,否则会丢失System.out.println()到System.out.format()。
Alternatively, you can use
或者,您可以使用
String outputString = String.format("format-string", arg1, arg2...);
to create an output String, and then use
创建一个输出字符串,然后使用
System.out.println(outputString);
as before to print it.
像以前一样打印它。
回答by Jonathan Feinberg
How about
怎么样
System.out.printf("%-30s %f\n", pt.toString(), conditionalProbs[1]);
See the docs for more information on the Formatter mini-language.
有关Formatter mini-language 的更多信息,请参阅文档。
回答by S.Lott
回答by daniel_or_else
Using j-text-utilsyou may print to console a table like:

使用j-text-utils您可以打印控制台,如:

And it as simple as:
它很简单:
TextTable tt = new TextTable(columnNames, data);
tt.printTable();
The API also allows sorting and row numbering ...
API 还允许排序和行编号......

