在控制台上正确对齐字符串 [英] Properly aligning Strings on the console

查看:66
本文介绍了在控制台上正确对齐字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个程序,它遍历一个 ArrayList 并使用 System.out 将各种手机相关信息打印到控制台.

I have this program that iterates through an ArrayList and prints out various cellphone related information to the console using 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));

我遇到的问题是,名称较长的客户推"了空格字符串,导致对齐混乱.有什么办法可以使对齐一致?

The problem I have is, Customers with longer names 'push' the space strings causing the alignment to mess up. Is there any way I can get the alignment to be uniform?

推荐答案

首先需要计算每列的最大宽度...

First, you need to calculate the maximum width of each column...

一旦你有了这些信息,你可以简单地使用 String.format("$-" + columnWidth + "s", c.getName()) 来生成一个 Stringcode> 值将填充 String 使其符合 columnWidth 要求

Once you have this information, you could simply use String.format("$-" + columnWidth + "s", c.getName()) to generate a String value that would pad the String so that it matched the columnWidth requirements

这是我从上一个关于类似主题的问题中修改的示例.

This is an example I modified from a previous question on a simular subject.

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     

这篇关于在控制台上正确对齐字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆