Java:更漂亮的打印?

Nic*_*ner 10 java console pretty-print

在我的计算结束时,我打印结果:

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();
}
Run Code Online (Sandbox Code Playgroud)

例如,这会产生:

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
Run Code Online (Sandbox Code Playgroud)

只是把两个\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
Run Code Online (Sandbox Code Playgroud)

(注意:我很难让SO文本编辑器完美地排列这些列,但希望你能得到这个想法.)

有没有一种简单的方法可以做到这一点,或者我必须编写一个方法来尝试根据"树"列中字符串的长度来计算出来吗?

fal*_*tro 15

你正在寻找场长.试试这个:

printf ("%-32s %f\n", pt.toString(), conditionalProbs[1])
Run Code Online (Sandbox Code Playgroud)

-32告诉你字符串应该左对齐,但是字段长度为32个字符(根据你的喜好调整,我选择了32,因为它是8的倍数,这是终端上的正常制表位).在标题上使用相同的标题,但%s代替%f将使得一行也很好.