小编典典

在控制台上正确对齐字符串

java

我有这个程序,它遍历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));

我的问题是,名称较长的客户会“推”空格字符串,导致对齐混乱。有什么办法可以使对齐方式一致吗?


阅读 337

收藏
2020-11-30

共1个答案

小编典典

首先,您需要计算每列的最大宽度…

掌握这些信息后,您可以简单地使用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
2020-11-30