Results 1 to 2 of 2

Thread: Java string format

  1. #1
    Join Date
    Jan 2009
    Location
    Denmark
    Beans
    Hidden!
    Distro
    Ubuntu 12.04 Precise Pangolin

    Java string format

    Hey guys.

    I have a program where i query a MS-SQL database and returns a result set with about 150 entries, in 12 cells.

    My question is, how can i format the returning result in a table format in a console program?

    See example in picture since the editor cant display as i want it. Thanks.

    I have looked at System.out.printf and System.out.format but i cant wrap my head around it ;/ So hoping someone can help me out or point me in the right direction.

    Thanks in advance.
    Kind regards.
    Attached Images Attached Images

  2. #2
    Join Date
    Jun 2007
    Location
    Maryland, US
    Beans
    6,279
    Distro
    Kubuntu

    Re: Java string format

    Quote Originally Posted by Drenriza View Post
    See example in picture since the editor cant display as i want it. Thanks.
    Baloney... use CODE tags to preserve output.

    See if this helps:
    Code:
    public class Format
    {
        public static void main(String[] args)
        {
            String[] raw = { "1", "222", "33333333", "44444", "5555", "6666666", "777777",
                             "888888", "999999", "11", "22", "3333", "444", "5555555", "666",
                             "7777777", "88888888", "9999", "111", "222", "333333", "4444444",
                             "555", "6666666", "7777777", "888888", "99999" };
    
            for (int i = 0; i < raw.length; ++i)
            {
                if (i % 9 == 0) System.out.println();
    
                System.out.format("%-8s ", raw[i]);
            }
            System.out.println();
        }
    }
    Output looks like:
    Code:
    1        222      33333333 44444    5555     6666666  777777   888888   999999   
    11       22       3333     444      5555555  666      7777777  88888888 9999     
    111      222      333333   4444444  555      6666666  7777777  888888   99999

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •