Java:更简单的漂亮打印?

发布于 2024-08-15 02:12:12 字数 885 浏览 7 评论 0 原文

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

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();
}

例如,这会产生:

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

仅将两个 \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

注意:我很难让 SO 文本编辑器完美地排列这些列,但希望你明白这个想法。)

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

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 \t in there is sort of clumsy - the columns don't really line up. I'd rather have an output like this:

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.)

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

不甘平庸 2024-08-22 02:12:12

您正在寻找字段长度。尝试使用这个:

printf ("%-32s %f\n", pt.toString(), conditionalProbs[1])

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

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 %s instead of %f will make that one line up nicely too.

泅人 2024-08-22 02:12:12

您需要的是令人惊叹且免费的 format()

它的工作原理是让您在模板字符串中指定占位符;它产生模板和值的组合作为输出。

示例:

System.out.format("%-25s %9.7f%n", "K and Burstner", 0.055170);
  • %s 是字符串的占位符;
  • %25s 表示将任何给定字符串空白填充到 25 个字符。
  • %-25s 表示将字段中的字符串左对齐,即填充到字符串的右侧。
  • %9.7f表示输出一个浮点数,共9位,小数点右边7位。
  • %n 是“执行”行终止所必需的,否则当您从 System.out.println() 转到 System 时,您会错过这一点.out.format()。

或者,您可以使用

String outputString = String.format("format-string", arg1, arg2...);

创建一个输出字符串,然后

System.out.println(outputString);

像以前一样使用它来打印它。

What you need is the amazing yet free 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);
  • %s is a placeholder for Strings;
  • %25s means blank-pad any given String to 25 characters.
  • %-25s means left-justify the String in the field, i.e. pad to the right of the string.
  • %9.7f means output a floating-point number with 9 places in all and 7 to the right of the decimal.
  • %n is necessary to "do" a line termination, which is what you're otherwise missing when you go from System.out.println() to 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.

静待花开 2024-08-22 02:12:12

如何

System.out.printf("%-30s %f\n", pt.toString(), conditionalProbs[1]);

查看文档有关格式化程序迷你语言

How about

System.out.printf("%-30s %f\n", pt.toString(), conditionalProbs[1]);

See the docs for more information on the Formatter mini-language.

感情洁癖 2024-08-22 02:12:12

使用 j-text-utils 您可以打印来控制台如下表格:

就这么简单:

TextTable tt = new TextTable(columnNames, data);                                                         
tt.printTable();   

API 还允许排序和行编号...

Using j-text-utils you may print to console a table like:

And it as simple as:

TextTable tt = new TextTable(columnNames, data);                                                         
tt.printTable();   

The API also allows sorting and row numbering ...

ぃ双果 2024-08-22 02:12:12

也许java.io.PrintStreamprintf 和/或 format 方法就是您正在寻找的...

Perhaps java.io.PrintStream's printf and/or format method is what you are looking for...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文