如何使用printf对齐

时间:2014-10-23 06:33:22

标签: java printing printf

我有两个printf它们是分开的,因为一个正在循环我无法对齐其中一个printf。我希望它在朋友面前排队。

什么是打印

Member                     Friends             
Chi Cho                 Joe Blow
                 Jimmy Brown
Status: 

Joe Blow                 John Ko
Status: Coding like a friend

Tammy Joe                 Joe Johnson
Status: this is great

Bing Smith                 John Brown
Status: This sucks

我想要的是什么

Member                     Friends             
Chi Cho                    Joe Blow
                           Jimmy Brown
Status: 

Joe Blow                   John Ko
Status: Coding like a friend

Tammy Joe                  Joe Johnson
Status: this is great

Bing Smith                 John Brown
Status: This sucks

for (int i = 0 ; i< profiles; i++)
    {
    String arrayName = face.get(i).getName();       
    String statusName = face.get(i).getStatus();

    LinkedList<String> linkName = face.get(i).getFriend();
    String realname ="";
    System.out.printf("%-5.20s" ,arrayName);
    for(String toname : linkName)   
    System.out.printf("                 %-5.20s%n" ,toname);    
    System.out.println("Status: " + statusName);
    System.out.println();
    }

2 个答案:

答案 0 :(得分:0)

我使用以下代码(我将linkName变量更改为String数组,将face类更改为Strings数组的ArrayList以使其在我的计算机上运行):

public static void main(String[] args) {
    ArrayList<String[]> profiles = new ArrayList<>();
    profiles.add(new String[] { "John", "Elisabeth", "John Snow" });
    profiles.add(new String[] { "John Snow", "Elisabeth W.", "George Palin" });
    profiles.add(new String[] { "John Eduard", "Annabelle", "Mike" });

    for (int i = 0 ; i < profiles.length; i++) {
        String arrayName = "Name";       
        String statusName = "Married";

        String[] linkName = profiles.get(i);
        String realname = "";

        System.out.printf("%-20s", arrayName);

        for (String toname : linkName) {
            System.out.printf("%-20s", toname);
            System.out.println();

            // If the current toname isn't the last element in the linkName array.
            if (toname != linkName[linkName.length - 1]) {
                System.out.printf("%-20s", "");
            }
        }

        System.out.println("Status: " + statusName);
        System.out.println();
    }
}

输出:

Name                John
                    Elisabeth
                    John Snow
Status: Married

Name                John Snow
                    Elisabeth W.
                    George Palin
Status: Married

Name                John Eduard
                    Annabelle
                    Mike
Status: Married

编辑:这是对您的代码的实际更正:

for (int i = 0 ; i < profiles; i++) {
    String arrayName = face.get(i).getName();       
    String statusName = face.get(i).getStatus();

    LinkedList<String> linkName = face.get(i).getFriend();
    String realname = "";
    System.out.printf("%-20s", arrayName);

    for (String toname : linkName) {
        System.out.printf("%-20s", toname);
        System.out.println();

        if (toname != linkName.get(linkName.length - 1)) {
            System.out.printf("%-20s", "");
        }
    }   

    System.out.println("Status: " + statusName);
    System.out.println();
}

答案 1 :(得分:0)

尝试使用 System.out.println()包含您想要的多个制表符(8个空格)空格 System.out.println("Member" "friends")如果您愿意,可以使用+运算符

添加您的ans