CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 12 of 12
  1. #1
    Join Date
    Dec 2009
    Posts
    7

    [RESOLVED] Text formating

    I want to print out some thing like this. Ignore the dots.

    Title1..................Title2......................Title3
    Value1 = 22.3.......Value2 = 223.55........Value3 = 5.634
    Value10 = 345.9....Value11 = 8465.340...Value12 = 123.342

    I want everything aligning in columns.
    I need to specificy how many decimal points I want.
    A bonus would be to make all the = align.

    I played around with import java.util.*; but I don't know it well enough.

  2. #2
    dlorde is offline Elite Member Power Poster
    Join Date
    Aug 1999
    Location
    UK
    Posts
    10,163

    Re: Text formating

    If you search this forum you'll find a printed column alignment routine that someone (Keang?) posted here.

    The way I'd do it would be to write a method that takes the required column width and the String of text to be padded out, and returns a String of text padded out with the required character (could be passed as another argument) to the required length.

    Then you can assemble a row to print by calling the method for each column item - or print each item in turn using 'print(..)' instead of 'println(..)'.

    Simples...

    Optimism is an occupational hazard of programming: testing is the treatment...
    K. Beck
    Please use [CODE]...your code here...[/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.

  3. #3
    Join Date
    Dec 2009
    Posts
    7

    Re: Text formating

    I wrote a compilable version of my method. It works.
    dlorde, I don't understand what you are trying to say, maybe use simpler tearms, I'm new to Java.
    I can't find the post you are referring to. I had looked before in the forums, but there are just to many to look at.

    I am hopping for someone to help me find a simpler way. They're has to be a predefined method somewhere, a library, something!! I'm sure I'm not the first to want to this.


    Code:
    import java.text.DecimalFormat;
    
    class PrintTableFormat
     {
       public static void main(String[] args)
       {
         double values1[] = new double[] {10.5, 3.7, 234.3};
         double values2[] = new double[] {154.12, 43.7, 4.34};
         double values3[] = new double[] {1.14, 234.33, 67.95};
         
         int index = 0;                       
         int length = values1.length;         
         int x,y,z;                                        //Intermediate variables, used to find how many dozens are in the number
         int dozens1, dozens2, dozens3;                                                          //Used to store how many variables are in the number
         String spaces1 = "", spaces2 = "";                                                 //Used to store how many spaces to put in between columns
         String []titles = new String[] {"TitleNumber1 ", "TitleNumber2 ", "TitleNumber3 "};       //Titles, they have to be bigger then the numbers or else it does not work.
         int []displayFormatLengthMinimum = new int[] {5, 6};                                     //Its the minimum amout of characters before the number is printed. R0 = , is five Vn0 = , is 6. 
         int []valueDecimals = new int[] {1, 2};                                           //The amout of decimal points.
         int spacesReference1 = 0, spacesReference2 = 0;                                        //To store the number of spaces to put in the string spaces.
        
        
         DecimalFormat fmt1 = new DecimalFormat("##########0.0");
         DecimalFormat fmt2 = new DecimalFormat("#########0.00");
         
        
         while(index < titles.length)
         {
           System.out.print(titles[index]);
           index = index + 1;
         }
         System.out.println("");
          
         index = 0;
         while(index < length)
         {
           spacesReference1 = titles[0].length() - displayFormatLengthMinimum[0] - valueDecimals[0];
           spacesReference2 = titles[1].length() - displayFormatLengthMinimum[1] - valueDecimals[1];
          
           spaces1 = "";
           spaces2 = "";
          
           dozens1 = 0;
           dozens2 = 0;
           dozens3 = 0;
          
           x = (int)values1[index];
           y = (int)values2[index];
           z = index;
          
           if(z == 0)
           {
             z = 1;
           }
           
           if(y >= 0 && y < 1)
           {
             y = 1;
           }
          
           while(x != 0 || y != 0 || z != 0)
           {
             if(x != 0)
             {
               x = x / 10;
               dozens1 = dozens1 + 1;
             }
            
            if(y != 0)
            {
              y = y / 10;
              dozens2 = dozens2 + 1;
            }
            
            if(z != 0)
            {
              z = z / 10;
              dozens3 = dozens3 + 1;
            }
          }
         
          while((spacesReference1 - (dozens1 + 1) - (dozens3 - 1)) > 0 || ( spacesReference2 - (dozens2 + 1) - (dozens3 - 1)) > 0)
          {
            if((spacesReference1 - (dozens1 + 1) - (dozens3 - 1) != 0))
            {
              spaces1 = spaces1 + " ";
              spacesReference1 = spacesReference1 - 1;
            }
            
            if((spacesReference2 - (dozens2 + 1) - (dozens3 - 1)) != 0)
            {
              spaces2 = spaces2 + " ";
              spacesReference2 = spacesReference2 - 1;
            }
          }      
          
          System.out.println("R" + index + " = " + fmt1.format(values1[index]) + spaces1 + "Vn" + index + " = " + fmt2.format(values2[index]) + spaces2 + "Vr" + index + " = " + fmt2.format(values3[index]));
          index = index + 1;
        }
       }
     }
    Last edited by Tyrant_ACC; December 4th, 2009 at 01:33 AM.

  4. #4
    Join Date
    Nov 2009
    Posts
    18

    Re: Text formating

    http://i247.photobucket.com/albums/g...40/example.png

    Do you mean something like this? I can post the code for it if you want...

    And please edit your post with
    Code:
     // Code goes here...
    tags. Nobody can read that the way it is right now.
    Last edited by XDan; December 4th, 2009 at 01:28 AM.

  5. #5
    Join Date
    Dec 2009
    Posts
    7

    Re: Text formating

    Quote Originally Posted by XDan View Post
    http://i247.photobucket.com/albums/g...40/example.png

    Do you mean something like this? I can post the code for it if you want...

    And please edit your post with
    Code:
     // Code goes here...
    tags. Nobody can read that the way it is right now.
    Its done, and no, that is not exactly what I want.
    Compile the code I wrote and you will see what I want.
    My way works, but it seems like alot of trouble for nothing.
    Last edited by Tyrant_ACC; December 4th, 2009 at 01:55 AM.

  6. #6
    Join Date
    Nov 2009
    Posts
    18

    Re: Text formating

    Quote Originally Posted by Tyrant_ACC View Post
    Its done, and yes, that is exactly what I want. My way works, but seems like alot of trouble for nothing.
    Okay, well my solution is far less lines of code. Here it is:

    Code:
    public class Table {
    
     public static void main(String args[]) {
         
         double values1[] = new double[] {10.5, 3.7, 234.3};
         double values2[] = new double[] {154.12, 43.7, 4.34};
         double values3[] = new double[] {1.14, 234.33, 67.95};
         
         System.out.println();
         System.out.println("Title1" + "            Title2" + "             Title3");
         System.out.println("------" + "            ------" + "             ------");
         System.out.println("Value1 = " + values1[0] + "     Value2 = " + values1[1] + "       Value3 = " + values1[2]);
         System.out.println();
         System.out.println("Value4 = " + values2[0] + "   Value5 = " + values2[1] + "      Value6 = " + values2[2]);
         System.out.println();
         System.out.println("Value7 = " + values3[0] + "     Value8 = " + values3[1] + "    Value9 = " + values3[2]);
     }
      
    }

  7. #7
    Join Date
    Dec 2009
    Posts
    7

    Re: Text formating

    sorry, that is not what I want...
    compile my code and you will see what I want.

    here is a picture.

    All I want to know is if there is an obvious easier way to do this, and if so, point me in the right direction.
    Attached Images Attached Images  
    Last edited by Tyrant_ACC; December 4th, 2009 at 02:09 AM.

  8. #8
    Join Date
    Nov 2009
    Posts
    18

    Re: Text formating

    I compiled and ran your code and it pretty much has the same output as mine you're just displaying elements in the array in a table format with with everything aligned. I did the exact same thing just with fewer lines of code I don't see how this is any different.

    Explain more what you want it to do please?
    Last edited by XDan; December 4th, 2009 at 02:12 AM.

  9. #9
    Join Date
    Dec 2009
    Posts
    7

    Re: Text formating

    The numbers I put are set.
    In my program they are not set, and the decimals are infinite...or close to it.

    Once I get to the 11th row, Value10 will appear witch will bump everything up.

    Try it with these values.

    {10.5546, 3.72754, 234.37247}
    {154.122475, 43.72725, 4.34257}
    {1.142457, 234.33275, 67.95257}

    If you use those instead, the outputs from your version will be different then mine.
    I wrote a general version witch does everything I want. I feel like I did it the wrong way. There should be a simpler way.

    another picture.
    Attached Images Attached Images  
    Last edited by Tyrant_ACC; December 4th, 2009 at 02:20 AM.

  10. #10
    dlorde is offline Elite Member Power Poster
    Join Date
    Aug 1999
    Location
    UK
    Posts
    10,163

    Re: Text formating

    Quote Originally Posted by XDan View Post
    I compiled and ran your code and it pretty much has the same output as mine you're just displaying elements in the array in a table format with with everything aligned. I did the exact same thing just with fewer lines of code I don't see how this is any different.
    What happens to your formatting when arbitrary doubles with arbitrary numbers of digits are passed in to the program as parameters, or read from a file?

    Doing more things faster is no substitute for doing the right things...
    S. R. Covey
    Please use &#91;CODE]...your code here...&#91;/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.

  11. #11
    dlorde is offline Elite Member Power Poster
    Join Date
    Aug 1999
    Location
    UK
    Posts
    10,163

    Re: Text formating

    Quote Originally Posted by Tyrant_ACC View Post
    dlorde, I don't understand what you are trying to say, maybe use simpler tearms, I'm new to Java.
    OK. If I have it right, you want each column to be left aligned and separated from the previous column. To do this, you need to ensure that each column entry is printed the same width (just longer than the widest entry). To get each entry for a column to be the same width, you need to pad it out with spaces or some other character. To know how wide the column needs to be (how much padding you need), you need to know the width of the widest column entry.

    [If you just want to pick a column width that will be wide enough for any data, you can skip step 1]

    1. Find the required width for each column: for each column, go through each entry in the column, including the header, and find the length of the longest entry. To this maximum width, add the width of the space you want between the columns. So after this step you should have the required width for every column.

    2. Write a method that will take a text String and a width value as parameters, and will pad out the text with spaces (or '.' or whatever character you want) until it reaches the width required then return this text String.

    3. Print the column headers: for each column, get the header text and the column width you calculated in step 1. Pass the text and the width to the method you wrote in step 2. This will return you the header for the column, padded out to the width required. Add this text String to the header row String. When all the padded column headers have been added to the header row String, print the header row String.

    4. Print the data rows: for each row, get each column entry in turn and pad it out to the column width using the padding method, as you did in step 3 for the headers. Add each padded column entry to the row String. When all the padded column entries in the row have been added to the row String, print the row String.

    5. Stop.

    An alternative to printing each row as a single String, would be to print each column entry after it has been padded to length, using System.print instead of System.println.

    If you don't think carefully, you might believe that programming is just typing statements in a programming language...
    W. Cunningham
    Last edited by dlorde; December 4th, 2009 at 07:11 AM.
    Please use &#91;CODE]...your code here...&#91;/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.

  12. #12
    Join Date
    Dec 2009
    Posts
    7

    Re: Text formating

    Oh I understand now...
    I also ask my brother and he mentioned i could just use the tab character.
    Each column is 3 tabs wide....everything works from there...

    /t or /tab, he wasen't shure.

    Thanks for all the help.

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured