JJ72
JJ72

Reputation: 62

Formatting output java

I'm trying to get my output look a little better. As long as I keep it very simple (as shown below) it works fine, but when I put the information in an ArrayList and use a constructor, I get stuck.

public class printing2 {
    public static void main(String[] args) {

        System.out.format("%-10s%-15s%-15s",
            "LastName", "FirstName", "SocialNo");
        System.out.println();
        System.out.format("%-10s%-15s%-15s",
            "James", "Johnson", "12345678");        
    }
}

I have tried to Google this for some time now, and I have tried about a thousand different ways to solve this but I just can't seem to get it right.

Upvotes: 0

Views: 64

Answers (1)

JeramyRR
JeramyRR

Reputation: 4461

JJ72, You were close. This may be what you were looking for:

public class Main {
    public static void main(String[] args) {

        ArrayList<testPrint> list = new ArrayList<>();

        testPrint p1 = new testPrint("James", "Johnson", "12345678");

        list.add(p1);

        System.out.println(String.format("%-15s%-15s%-15s", "FirstName", "LastName", "SocNumber"));
        for (testPrint l : list) {
            System.out.println(String.format("%-15s%-15s%-15s", l.getFname(), l.getLname(), l.getnumber()));
        }
    }
}

Upvotes: 1

Related Questions