0

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.

4
  • 1
    Please share the code with issue Commented Nov 15, 2015 at 20:11
  • What output you're expecting? Commented Nov 15, 2015 at 20:11
  • If you want to see it like a table, you should use \t, but it is not clear what you want to do... Commented Nov 15, 2015 at 20:12
  • If you don't show us the code that isn't working, how are we supposed to be able to tell what you're doing wrong? Commented Nov 15, 2015 at 20:15

1 Answer 1

1

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()));
        }
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

AAhhhh. Yes! Thank you very much. I will have to put the format "inside" the println-statement and get. the variables I want to be printed out. Awsome :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.