Best way to format a chart for any length of string without using excel












1














I am trying to create a chart for my names, but it doesn't align correctly if the name is too short or too long. I want to know if there is way I can align the names no matter the length of the string. Below is the align method I used



public static final String FORMAT = "%st%s,%st%s%n";



public static void print() {
for(int i = 0; i < n; i++)
System.out.printf(FORMAT, name[i], last[i], first[i], formatName[i]);


output:



Nirvana Narayan Narayan,Nirvana Nirvana--Narayan
Kim Do Do,Kim Kim--Do
Kiri Wood Wood,Kiri Kiri--Wood
Quinn Hume Hume,Quinn Quinn--Hume









share|improve this question


















  • 1




    Use .length, take the maximum and print the missing spaces for shorter strings. In C you have %n, that gives you the number of characters printed so far, but I'm pretty sure Java does not have a similar format specifier.
    – KaeptnNemo
    Nov 19 at 22:09
















1














I am trying to create a chart for my names, but it doesn't align correctly if the name is too short or too long. I want to know if there is way I can align the names no matter the length of the string. Below is the align method I used



public static final String FORMAT = "%st%s,%st%s%n";



public static void print() {
for(int i = 0; i < n; i++)
System.out.printf(FORMAT, name[i], last[i], first[i], formatName[i]);


output:



Nirvana Narayan Narayan,Nirvana Nirvana--Narayan
Kim Do Do,Kim Kim--Do
Kiri Wood Wood,Kiri Kiri--Wood
Quinn Hume Hume,Quinn Quinn--Hume









share|improve this question


















  • 1




    Use .length, take the maximum and print the missing spaces for shorter strings. In C you have %n, that gives you the number of characters printed so far, but I'm pretty sure Java does not have a similar format specifier.
    – KaeptnNemo
    Nov 19 at 22:09














1












1








1







I am trying to create a chart for my names, but it doesn't align correctly if the name is too short or too long. I want to know if there is way I can align the names no matter the length of the string. Below is the align method I used



public static final String FORMAT = "%st%s,%st%s%n";



public static void print() {
for(int i = 0; i < n; i++)
System.out.printf(FORMAT, name[i], last[i], first[i], formatName[i]);


output:



Nirvana Narayan Narayan,Nirvana Nirvana--Narayan
Kim Do Do,Kim Kim--Do
Kiri Wood Wood,Kiri Kiri--Wood
Quinn Hume Hume,Quinn Quinn--Hume









share|improve this question













I am trying to create a chart for my names, but it doesn't align correctly if the name is too short or too long. I want to know if there is way I can align the names no matter the length of the string. Below is the align method I used



public static final String FORMAT = "%st%s,%st%s%n";



public static void print() {
for(int i = 0; i < n; i++)
System.out.printf(FORMAT, name[i], last[i], first[i], formatName[i]);


output:



Nirvana Narayan Narayan,Nirvana Nirvana--Narayan
Kim Do Do,Kim Kim--Do
Kiri Wood Wood,Kiri Kiri--Wood
Quinn Hume Hume,Quinn Quinn--Hume






java string






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 19 at 22:03









James P

102




102








  • 1




    Use .length, take the maximum and print the missing spaces for shorter strings. In C you have %n, that gives you the number of characters printed so far, but I'm pretty sure Java does not have a similar format specifier.
    – KaeptnNemo
    Nov 19 at 22:09














  • 1




    Use .length, take the maximum and print the missing spaces for shorter strings. In C you have %n, that gives you the number of characters printed so far, but I'm pretty sure Java does not have a similar format specifier.
    – KaeptnNemo
    Nov 19 at 22:09








1




1




Use .length, take the maximum and print the missing spaces for shorter strings. In C you have %n, that gives you the number of characters printed so far, but I'm pretty sure Java does not have a similar format specifier.
– KaeptnNemo
Nov 19 at 22:09




Use .length, take the maximum and print the missing spaces for shorter strings. In C you have %n, that gives you the number of characters printed so far, but I'm pretty sure Java does not have a similar format specifier.
– KaeptnNemo
Nov 19 at 22:09












1 Answer
1






active

oldest

votes


















1














As described by KaeptnNemo in a comment:




Use .length, take the maximum and print the missing spaces for shorter strings.




The easiest way to print missing spaces is to use the printf method with a format string using e.g. the %-10s format to print at least 10 characters, left-justified, filling with spaces as needed.



E.g. since the longest text for the first column is 23, a format string such as "%-23s %s%n" can be used. Now just write the code to build the format string automatically:



String input = { { "Nirvana Narayan Narayan", "Nirvana Nirvana--Narayan" },
{ "Kim Do Do", "Kim Kim--Do" },
{ "Kiri Wood Wood", "Kiri Kiri--Wood" },
{ "Quinn Hume Hume", "Quinn Quinn--Hume" } };

// Find column widths
int widths = new int[input[0].length];
for (String row : input)
for (int i = 0; i < widths.length; i++)
widths[i] = Math.max(widths[i], row[i].length());

// Build format string
StringBuilder buf = new StringBuilder();
for (int i = 0; i < widths.length - 1; i++)
buf.append("%-").append(widths[i]).append("s ");
String fmt = buf.append("%s%n").toString();

// Print table
for (String row : input)
System.out.printf(fmt, (Object) row);


Output



Nirvana Narayan Narayan Nirvana Nirvana--Narayan
Kim Do Do Kim Kim--Do
Kiri Wood Wood Kiri Kiri--Wood
Quinn Hume Hume Quinn Quinn--Hume




If you like, you can even add column-separators.



// Build format string
StringBuilder buf = new StringBuilder("|");
for (int i = 0; i < widths.length; i++)
buf.append(" %-").append(widths[i]).append("s |");
String fmt = buf.append("%n").toString();


Output



| Nirvana Narayan Narayan | Nirvana Nirvana--Narayan |
| Kim Do Do | Kim Kim--Do |
| Kiri Wood Wood | Kiri Kiri--Wood |
| Quinn Hume Hume | Quinn Quinn--Hume |





share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53383282%2fbest-way-to-format-a-chart-for-any-length-of-string-without-using-excel%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    As described by KaeptnNemo in a comment:




    Use .length, take the maximum and print the missing spaces for shorter strings.




    The easiest way to print missing spaces is to use the printf method with a format string using e.g. the %-10s format to print at least 10 characters, left-justified, filling with spaces as needed.



    E.g. since the longest text for the first column is 23, a format string such as "%-23s %s%n" can be used. Now just write the code to build the format string automatically:



    String input = { { "Nirvana Narayan Narayan", "Nirvana Nirvana--Narayan" },
    { "Kim Do Do", "Kim Kim--Do" },
    { "Kiri Wood Wood", "Kiri Kiri--Wood" },
    { "Quinn Hume Hume", "Quinn Quinn--Hume" } };

    // Find column widths
    int widths = new int[input[0].length];
    for (String row : input)
    for (int i = 0; i < widths.length; i++)
    widths[i] = Math.max(widths[i], row[i].length());

    // Build format string
    StringBuilder buf = new StringBuilder();
    for (int i = 0; i < widths.length - 1; i++)
    buf.append("%-").append(widths[i]).append("s ");
    String fmt = buf.append("%s%n").toString();

    // Print table
    for (String row : input)
    System.out.printf(fmt, (Object) row);


    Output



    Nirvana Narayan Narayan Nirvana Nirvana--Narayan
    Kim Do Do Kim Kim--Do
    Kiri Wood Wood Kiri Kiri--Wood
    Quinn Hume Hume Quinn Quinn--Hume




    If you like, you can even add column-separators.



    // Build format string
    StringBuilder buf = new StringBuilder("|");
    for (int i = 0; i < widths.length; i++)
    buf.append(" %-").append(widths[i]).append("s |");
    String fmt = buf.append("%n").toString();


    Output



    | Nirvana Narayan Narayan | Nirvana Nirvana--Narayan |
    | Kim Do Do | Kim Kim--Do |
    | Kiri Wood Wood | Kiri Kiri--Wood |
    | Quinn Hume Hume | Quinn Quinn--Hume |





    share|improve this answer




























      1














      As described by KaeptnNemo in a comment:




      Use .length, take the maximum and print the missing spaces for shorter strings.




      The easiest way to print missing spaces is to use the printf method with a format string using e.g. the %-10s format to print at least 10 characters, left-justified, filling with spaces as needed.



      E.g. since the longest text for the first column is 23, a format string such as "%-23s %s%n" can be used. Now just write the code to build the format string automatically:



      String input = { { "Nirvana Narayan Narayan", "Nirvana Nirvana--Narayan" },
      { "Kim Do Do", "Kim Kim--Do" },
      { "Kiri Wood Wood", "Kiri Kiri--Wood" },
      { "Quinn Hume Hume", "Quinn Quinn--Hume" } };

      // Find column widths
      int widths = new int[input[0].length];
      for (String row : input)
      for (int i = 0; i < widths.length; i++)
      widths[i] = Math.max(widths[i], row[i].length());

      // Build format string
      StringBuilder buf = new StringBuilder();
      for (int i = 0; i < widths.length - 1; i++)
      buf.append("%-").append(widths[i]).append("s ");
      String fmt = buf.append("%s%n").toString();

      // Print table
      for (String row : input)
      System.out.printf(fmt, (Object) row);


      Output



      Nirvana Narayan Narayan Nirvana Nirvana--Narayan
      Kim Do Do Kim Kim--Do
      Kiri Wood Wood Kiri Kiri--Wood
      Quinn Hume Hume Quinn Quinn--Hume




      If you like, you can even add column-separators.



      // Build format string
      StringBuilder buf = new StringBuilder("|");
      for (int i = 0; i < widths.length; i++)
      buf.append(" %-").append(widths[i]).append("s |");
      String fmt = buf.append("%n").toString();


      Output



      | Nirvana Narayan Narayan | Nirvana Nirvana--Narayan |
      | Kim Do Do | Kim Kim--Do |
      | Kiri Wood Wood | Kiri Kiri--Wood |
      | Quinn Hume Hume | Quinn Quinn--Hume |





      share|improve this answer


























        1












        1








        1






        As described by KaeptnNemo in a comment:




        Use .length, take the maximum and print the missing spaces for shorter strings.




        The easiest way to print missing spaces is to use the printf method with a format string using e.g. the %-10s format to print at least 10 characters, left-justified, filling with spaces as needed.



        E.g. since the longest text for the first column is 23, a format string such as "%-23s %s%n" can be used. Now just write the code to build the format string automatically:



        String input = { { "Nirvana Narayan Narayan", "Nirvana Nirvana--Narayan" },
        { "Kim Do Do", "Kim Kim--Do" },
        { "Kiri Wood Wood", "Kiri Kiri--Wood" },
        { "Quinn Hume Hume", "Quinn Quinn--Hume" } };

        // Find column widths
        int widths = new int[input[0].length];
        for (String row : input)
        for (int i = 0; i < widths.length; i++)
        widths[i] = Math.max(widths[i], row[i].length());

        // Build format string
        StringBuilder buf = new StringBuilder();
        for (int i = 0; i < widths.length - 1; i++)
        buf.append("%-").append(widths[i]).append("s ");
        String fmt = buf.append("%s%n").toString();

        // Print table
        for (String row : input)
        System.out.printf(fmt, (Object) row);


        Output



        Nirvana Narayan Narayan Nirvana Nirvana--Narayan
        Kim Do Do Kim Kim--Do
        Kiri Wood Wood Kiri Kiri--Wood
        Quinn Hume Hume Quinn Quinn--Hume




        If you like, you can even add column-separators.



        // Build format string
        StringBuilder buf = new StringBuilder("|");
        for (int i = 0; i < widths.length; i++)
        buf.append(" %-").append(widths[i]).append("s |");
        String fmt = buf.append("%n").toString();


        Output



        | Nirvana Narayan Narayan | Nirvana Nirvana--Narayan |
        | Kim Do Do | Kim Kim--Do |
        | Kiri Wood Wood | Kiri Kiri--Wood |
        | Quinn Hume Hume | Quinn Quinn--Hume |





        share|improve this answer














        As described by KaeptnNemo in a comment:




        Use .length, take the maximum and print the missing spaces for shorter strings.




        The easiest way to print missing spaces is to use the printf method with a format string using e.g. the %-10s format to print at least 10 characters, left-justified, filling with spaces as needed.



        E.g. since the longest text for the first column is 23, a format string such as "%-23s %s%n" can be used. Now just write the code to build the format string automatically:



        String input = { { "Nirvana Narayan Narayan", "Nirvana Nirvana--Narayan" },
        { "Kim Do Do", "Kim Kim--Do" },
        { "Kiri Wood Wood", "Kiri Kiri--Wood" },
        { "Quinn Hume Hume", "Quinn Quinn--Hume" } };

        // Find column widths
        int widths = new int[input[0].length];
        for (String row : input)
        for (int i = 0; i < widths.length; i++)
        widths[i] = Math.max(widths[i], row[i].length());

        // Build format string
        StringBuilder buf = new StringBuilder();
        for (int i = 0; i < widths.length - 1; i++)
        buf.append("%-").append(widths[i]).append("s ");
        String fmt = buf.append("%s%n").toString();

        // Print table
        for (String row : input)
        System.out.printf(fmt, (Object) row);


        Output



        Nirvana Narayan Narayan Nirvana Nirvana--Narayan
        Kim Do Do Kim Kim--Do
        Kiri Wood Wood Kiri Kiri--Wood
        Quinn Hume Hume Quinn Quinn--Hume




        If you like, you can even add column-separators.



        // Build format string
        StringBuilder buf = new StringBuilder("|");
        for (int i = 0; i < widths.length; i++)
        buf.append(" %-").append(widths[i]).append("s |");
        String fmt = buf.append("%n").toString();


        Output



        | Nirvana Narayan Narayan | Nirvana Nirvana--Narayan |
        | Kim Do Do | Kim Kim--Do |
        | Kiri Wood Wood | Kiri Kiri--Wood |
        | Quinn Hume Hume | Quinn Quinn--Hume |






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 19 at 22:36

























        answered Nov 19 at 22:30









        Andreas

        74.7k458122




        74.7k458122






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53383282%2fbest-way-to-format-a-chart-for-any-length-of-string-without-using-excel%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            "Incorrect syntax near the keyword 'ON'. (on update cascade, on delete cascade,)

            Alcedinidae

            RAC Tourist Trophy