Search code examples
javaarraylistformatprintftabular

Printf output for arrayList to table with proper indent


I am basically done with my assignment but there's a little formatting required in the output and I just don't understand how I can use printf to solve the issue. I never had to use it before.

I have the user input ids and pws, that get saved in an array list.

Output should look like this:

    Nr. Name                               Password (ROT13)
-----------------------------------------------------------
 1. Sally Hu                               nopNOP
 2. Bernd das Brot                         zrvaCnffjbeg
 3. John Doe                               klmTRURVZ
-----------------------------------------------------------

but looks like this for me:

 Nr. Name                                 Password (ROT13)
-----------------------------------------------------------
 1. Sally Hu                               nopNOP
 2. Bernd das Brot                             zrvaCnffjbeg
 3. John Doe                               klmTRURVZ
-----------------------------------------------------------

Current code for the print:

  System.out.println("Nr. Name                       Passwort (ROT13)");


    System.out.println("-----------------------------------------------------------");
                    for(int i = 0; i < userList.size();i++)
                    {
                        System.out.println((userList.indexOf(userList.get(i))+1) +". " + userList.get(i).getmId() + "       " + rot13(userList.get(i).getmPw()));    
                    }

If someone could help me out real quick and maybe even explain some of the used printf functions used to solve this formatting problem? (So I can properly format with printf in the future, instead of this hacky pseudo formatting using +" "+ in my printlines that leads to problems like here)


Solution

  • I just don't understand how I can use printf

    Here is the method's signature, from the docs.

    public PrintStream printf(String format, Object... args)
    

    Let's leave aside the returned value and focus on the arguments:

    format is a string that specifies the formatting to be used and args is a list of the variables to be printed using that formatting.

    Let's take an example.

    Suppose you want to print a string in this specific format -> "5 + 6 = 11". But this is a specific instance of this formatted string, with these specific numbers, you instead want to put arbitary numbers and the result would be a string that shows the addition of those numbers.

    The string format is this: a number, a space ' ', the plus sign '+', a number, a space, the equals sign '=', a space and finaly one more number. In order for printf() to understand this you need to supply it as a string in the first argument.

    This is that string: String format = "%d + %d = %d"

    printf() will know the format you want from that string above and it will replace every '%d' (%f for floats, %s for strings etc) with an integer variable. Those are the integers that you supple the printf() after.

    Finally, at our specific example again, "5 + 6 = 11".

    int number1 = 5, number2 = 6;
    String format = "%d + %d = %d";
    //print the result formatted
    System.out.printf(format,number1, number2, number1+number2);
    

    You can change the values of number1 and number2, the result will be always their addition in the format that String format specifies.