0

I have two printf they are separate because one is doing a loop I am having trouble aligning one of the printf. I want it tho line up under friends.

What is printing

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

what I am trying to get

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();
    }
hue manny
  • 239
  • 2
  • 19

2 Answers2

0

I got it working with the following code (I changed the linkName variable to a String array and the face class to an ArrayList of those arrays of Strings to get it working on my computer):

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();
    }
}

output:

Name                John
                    Elisabeth
                    John Snow
Status: Married

Name                John Snow
                    Elisabeth W.
                    George Palin
Status: Married

Name                John Eduard
                    Annabelle
                    Mike
Status: Married

EDIT: Here is the actual correction to your code:

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();
}
Olavi Mustanoja
  • 2,045
  • 2
  • 23
  • 34
  • Thanks but i need the if not statement as if it was a Linked list not an array. Would you know how to do that? – hue manny Oct 23 '14 at 12:35
  • Of course, you just have to change the correct variables with the ones you want. Well, I'll edit in the code that should work for you. – Olavi Mustanoja Oct 23 '14 at 12:55
0

try using System.out.println()with as many tab(8 spaces) spaces you want like
System.out.println("Member" "friends") you can add your ans in between if you want by using the + operator