我有这个程序,它遍历ArrayList并使用System.out将各种与手机相关的信息打印到控制台。
for(Customer c: customers)
System.out.println(c.getNumber() + " " + c.getName() + " " + out + " " + in
+ " " + c.getPlan().getMinutesAllowed() + " " + c.getPlan().getMinutesUsed()
+ " " + over + " " + base + " " + extra + " " + String.format("%.2f" ,hst)
+ " " + "$" + String.format("%.2f" ,total));
我的问题是,名称较长的客户会“推”空格字符串,导致对齐混乱。有什么办法可以使对齐方式一致吗?
首先,您需要计算每列的最大宽度…
掌握这些信息后,您可以简单地使用String.format("$-" + columnWidth + "s", c.getName())
生成一个String
值来填充String
,使其符合columnWidth
要求
这是我从先前关于类比主题的问题所修改的一个示例。
import java.util.Random;
public class SalaryColumns {
public static void main(String[] args) {
int people = 20;
int month = 12;
String monthLabel = "Month #";
String personLabel = "Person #";
Random r = new Random(System.currentTimeMillis());
int[][] table = new int[people][month];
int[] columWidths = new int[month + 1];
columWidths[0] = personLabel.length() + Integer.toString(people).length() + 1;
// Load the table with values
for (int i = 0; i < table.length; i++) {
for (int j = 0; j < table[i].length; j++) {
table[i][j] = r.nextInt(20000 - 1000) + 1000;
columWidths[j + 1] = Math.max(
columWidths[j + 1],
Math.max(
monthLabel.length() + Integer.toString(month).length() + 1,
Integer.toString(table[i][j]).length() + 2));
}
}
// Print the table
System.out.println("\n\nSummer Internship Salary Information:");
StringBuilder sb = new StringBuilder(128);
sb.append(String.format("%-" + columWidths[0] + "s", ""));
for (int i = 0; i < month; i++) {
String label = monthLabel + i;
sb.append(String.format("%-" + columWidths[i + 1] + "s", label));
}
System.out.println(sb);
for (int i = 0; i < table.length; i++) {
sb = new StringBuilder(128);
String label = personLabel + i;
sb.append(String.format("%-" + columWidths[0] + "s", label, i));
for (int j = 0; j < table[i].length; j++) {
label = String.format("$%d", table[i][j]);
sb.append(String.format("%-" + columWidths[j + 1] + "s", label));
}
System.out.println(sb);
}
}
}
这将生成并输出类似的
Summer Internship Salary Information:
Month #0 Month #1 Month #2 Month #3 Month #4 Month #5 Month #6 Month #7 Month #8 Month #9 Month #10 Month #11
Person #0 $2574 $9670 $2962 $6078 $10177 $19115 $19648 $18317 $10943 $4696 $13920 $15849
Person #1 $11186 $1088 $16347 $4398 $4261 $5716 $13349 $13295 $15037 $2908 $14684 $18583
Person #2 $13291 $17601 $18549 $8453 $14743 $9698 $16769 $10189 $13406 $12953 $1183 $4015
Person #3 $15267 $11960 $8204 $5183 $5284 $4502 $7461 $18048 $1339 $6872 $7541 $4837
Person #4 $12197 $6997 $6393 $5430 $19472 $15211 $15697 $6243 $10941 $19776 $12378 $17240
Person #5 $13804 $12150 $9510 $8789 $12103 $7701 $11868 $10609 $2551 $4686 $14884 $4947
Person #6 $17787 $11562 $12459 $11820 $6855 $8461 $15972 $16140 $4808 $10483 $3528 $18031
Person #7 $6869 $16093 $19924 $4364 $18714 $16375 $4985 $19810 $11014 $9793 $12177 $12239
Person #8 $14821 $7640 $5461 $14503 $16152 $2632 $10500 $14565 $6303 $10049 $6213 $11225
Person #9 $7296 $3507 $19277 $11830 $6849 $11272 $3332 $12359 $17010 $15764 $15426 $9525
Person #10 $7542 $17571 $1951 $12967 $13560 $1671 $15344 $19383 $16083 $3292 $15838 $6757
Person #11 $14889 $1867 $9808 $7842 $3781 $9847 $18432 $6435 $1180 $2631 $14003 $19504
Person #12 $19333 $5576 $7874 $16040 $1138 $3775 $6694 $16931 $10638 $5016 $1569 $15356
Person #13 $9734 $9198 $16951 $18648 $12077 $14303 $10866 $17447 $12944 $6770 $16061 $3934
Person #14 $8688 $14968 $6624 $4663 $2301 $11669 $10259 $9038 $18287 $5481 $3782 $16952
Person #15 $9840 $12727 $6733 $16098 $1526 $19546 $3177 $17483 $5492 $9527 $16152 $3975
Person #16 $16208 $9491 $11815 $15529 $3927 $15393 $7070 $11037 $11221 $2784 $2665 $8375
Person #17 $9265 $19807 $12623 $17219 $16500 $16150 $7500 $10570 $5234 $7971 $10209 $10413
Person #18 $19993 $2573 $13955 $16018 $3127 $4256 $14105 $14600 $3024 $13789 $5543 $19016
Person #19 $8468 $15645 $7791 $4266 $16991 $9139 $7383 $7249 $7234 $19235 $8093 $5964
问题内容: 我有一个可以打印utf-8编码的字符串的字符串,其中带有特殊字符。 由于该exe是从控制台窗口使用的,因此它的输出被修改了,因为Windows使用了编码(aka )。 您将如何确保go 为控制台窗口打印正确编码的字符串,例如,打印: 而不是(不对正确的字符集进行任何翻译) 问题答案: 自2016年以来,您现在(2017年)就可以考虑使用,该编码随附了编码charmap,包括ISO-88
有没有办法避免java.lang.Process阻塞控制台?在我的情况下,当我执行以下代码时,我的输入在控制台中被阻止。所以我几乎再也不能输入任何东西了。我已经试过了。例如,在新线程中启动它是不起作用的。
问题 你想通过某种对齐方式来格式化字符串 解决方案 对于基本的字符串对齐操作,可以使用字符串的 ljust() , rjust() 和 center() 方法。比如: >>> text = 'Hello World' >>> text.ljust(20) 'Hello World ' >>> text.rjust(20) ' Hello World' >>> tex
问题内容: 控制台输入(win),字符集转换如何工作? 以下代码是非ascii字符输出垃圾-下例中的InputStreamReader不会将charset作为参数。 与操作系统无关,Java如何解决与控制台提示输入有关的所有不同字符集配置? 问题答案: 实际上, Java根本无法解决这个问题 。 它仅假设控制台编码与系统默认编码相同。这个假设在Windows系统上是错误的,因此Java不能提供很好
问题内容: 我有这些数字的数组 我使用这样的对象 得到这些结果 但是我需要像下面这样将结果对齐到正确的位置 您的帮助已经受到赞赏。 问题答案: 您可以将其包装成这样的呼叫:
我有一个几乎到处都是的窗口。我正在向它添加大按钮(带有图像),我希望它们继续向下添加,这就是为什么我使用并设置每次我添加一个按钮。 但问题是,当我添加不到4个按钮时,它们会对齐到行的中心,而不是我想要的左侧。就像这样 你知道我该怎么把它们对准左边吗?我试过gbc。锚定和gbc。填充但没有运气。谢谢 这是我的