Java 输出文件格式

发布于 2024-11-04 03:51:31 字数 988 浏览 1 评论 0原文

我在格式化输出文件以使其易于阅读时遇到了一些麻烦。我正在尝试输出文件,例如:

Name of School                   Size of School
--------------                   --------------
Blblah blah                        1300
Blah blah blah                    11300
Blah blah blah asdf               14220
Blah bblah                         1300

但遇到麻烦。目前,我正在使用以下代码来获取以下输出:

File file1 = new File("src\\sortedInt.txt");
Formatter fmt = new Formatter(file1);

HelperMethods.quickSortStudents(colleges, 0, colleges.length - 1);
for(int i = 0; i < colleges.length; i++)
{
      fmt.format(colleges[i].nameOfSchool + "%20d" + "\n", (int)colleges[i].numOfStudents);
      fmt.flush();
}  

这给了我:

eastman school of music                 800
clark university                1100
walla walla college                1100
drew                1200
juilliard                1200

因为我只是从大学名称的末尾开始填充。是否有办法填充整个字符串,以便所有字符串的长度都是恒定的?

谢谢大家的帮助

I am having a bit of trouble formatting an output file for myself to be easily readable. I am trying to output the file such as:

Name of School                   Size of School
--------------                   --------------
Blblah blah                        1300
Blah blah blah                    11300
Blah blah blah asdf               14220
Blah bblah                         1300

but am having trouble. Currently I am using the following code to get the following output:

File file1 = new File("src\\sortedInt.txt");
Formatter fmt = new Formatter(file1);

HelperMethods.quickSortStudents(colleges, 0, colleges.length - 1);
for(int i = 0; i < colleges.length; i++)
{
      fmt.format(colleges[i].nameOfSchool + "%20d" + "\n", (int)colleges[i].numOfStudents);
      fmt.flush();
}  

which gives me:

eastman school of music                 800
clark university                1100
walla walla college                1100
drew                1200
juilliard                1200

Because I am just padding from the end of the college name. Is there anyway to pad the whole string so all the strings are a constant length?

Thank you everyone for you help

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

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

发布评论

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

评论(2

巴黎夜雨 2024-11-11 03:51:31

是的,输出你的大学名称左对齐并填充到一定的长度,然后输出你的学生人数右对齐并填充到一定的长度:

fmt.format("%-20s%10d\n", colleges[i].nameOfSchool, (int) colleges[i].numOfStudents);

Yep, output your college name left-justified and padded to a certain length, then output your number of student right-justified and padded to a certain length:

fmt.format("%-20s%10d\n", colleges[i].nameOfSchool, (int) colleges[i].numOfStudents);
黒涩兲箜 2024-11-11 03:51:31

您可能还想添加要格式化的学校名称,如下所示:

fmt.format("%1$20s %1$5d\n", colleges[i].nameOfSchool, (int)colleges[i].numOfStudents);

以下链接可以让您更深入地了解格式化:
http://download.oracle.com/javase /1.5.0/docs/api/java/util/Formatter.html

You might want to add the name of the schools to be formatted too, as the following:

fmt.format("%1$20s %1$5d\n", colleges[i].nameOfSchool, (int)colleges[i].numOfStudents);

The following link can give you more insight in formatting:
http://download.oracle.com/javase/1.5.0/docs/api/java/util/Formatter.html

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