Results 1 to 16 of 16
  1. #1
    coolman50544 is offline Member
    Join Date
    Apr 2014
    Location
    Miami, FL
    Posts
    47
    Rep Power
    0

    Default How can I make a table nice and neat.

    So I just processed this rectangular array and made myself a table. I want to put a label for each column to identify what the values are. My attempt at this failed miserably, because it looks horrible. Here is the necessary code.

    Java Code:
    System.out.println();
                System.out.println("FORMATTED RESULTS");
                System.out.println(results);
                
                values[rowCount][0] = currency.format(monthlyInvestment);
                values[rowCount][1] = percent.format(interestRate/100);
                values[rowCount][2] = Integer.toString(years);
                values[rowCount][3] = currency.format(futureValue);
                rowCount++;
                
                // see if the user wants to continue
                System.out.print("Continue? (y/n):");
                System.out.println();
                choice = sc.next();
            }
            
            System.out.println("RECORED RESULTS \n MontlyInvestment Yearly Interest Rate Years Future Value");
            valuesProcessor:
            for(String[] row : values)
            {
                for(String col : row)
                {
                    if(col == null)
                        break valuesProcessor;
                    else
                        System.out.print(col + " ");         
                }
                System.out.println();
            }
        }
    If you are to compile this yourself, you would see the horrible output at the end. I would like to know how to properly align each label with its corresponding column.

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,391
    Rep Power
    25

    Default Re: How can I make a table nice and neat.

    Are you asking about characters that are printed on the console?

    One way to align characters is to use the printf() method with a formatting String that puts the data in a fixed column width.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    coolman50544 is offline Member
    Join Date
    Apr 2014
    Location
    Miami, FL
    Posts
    47
    Rep Power
    0

    Default Re: How can I make a table nice and neat.

    Quote Originally Posted by Norm View Post
    Are you asking about characters that are printed on the console?

    One way to align characters is to use the printf() method with a formatting String that puts the data in a fixed column width.
    Yes, this is what I am asking about. How can I use the printf() method and its parameters?

  4. #4
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,391
    Rep Power
    25

    Default Re: How can I make a table nice and neat.

    What have you tried? There are lots of examples around. Do a search.

    Also look at the String class's format() method. Its API doc has a link to some examples.
    Last edited by Norm; 05-28-2014 at 12:08 AM.
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    coolman50544 is offline Member
    Join Date
    Apr 2014
    Location
    Miami, FL
    Posts
    47
    Rep Power
    0

    Default Formatting a table of various strings

    I have an array that is receiving different values which are input from the user. Calculations are made from these values, and I want to show the recorded results of each value. Here is how it looks like:
    How can I make a table nice and neat.-capture.png
    Now here is the problem that makes me feel like ripping my hair out. I am using the printf() method to correctly align each column. This works fine, until the user enters a number with a different number of digits. This completely ruins the spacing between the values, because the printf() method will now move the string 4 characters to the right from a different area forif a new digit is occupying that space. I don't know how to fix this at all.
    Here is the code:
    Java Code:
    values[rowCount][0] = currency.format(monthlyInvestment);
                values[rowCount][1] = percent.format(interestRate/100);
                values[rowCount][2] = Integer.toString(years);
                values[rowCount][3] = currency.format(futureValue);
                rowCount++;
                
                // see if the user wants to continue
                System.out.print("Continue? (y/n):");
                System.out.println();
                choice = sc.next();
            }
            
            System.out.println("RECORED RESULTS \nInv/Mo.   Rate    Years  Future Value");
            valuesProcessor:
            for(String[] row : values)
            {
                int counterVariable = 0;
                for(String col : row)
                {
                    if(col == null)
                        break valuesProcessor;
                    else if(counterVariable == 0)
                        System.out.printf("%s", col);
                    else if(counterVariable == 1)
                        System.out.printf("%s", col);
                    else if(counterVariable == 2)
                        System.out.printf("%s", col);
                    else if(counterVariable == 3)
                        System.out.printf("%s", col);
                    System.out.print(" ");
                    counterVariable++;
                }
                System.out.println();
            }
    Somebody please help me to end my frustration.

  6. #6
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,391
    Rep Power
    25

    Default Re: How can I make a table nice and neat.

    Make the spacing large enough to hold what the user can enter.

    I don't see where the posted code is setting the width of the columns.
    If you don't understand my response, don't ignore it, ask a question.

  7. #7
    coolman50544 is offline Member
    Join Date
    Apr 2014
    Location
    Miami, FL
    Posts
    47
    Rep Power
    0

    Default Re: How can I make a table nice and neat.

    Quote Originally Posted by Norm View Post
    Make the spacing large enough to hold what the user can enter.

    I don't see where the posted code is setting the width of the columns.
    Just tried that. It is still not working correctly, and here are the results.
    Attachment 6209Attachment 6209Attachment 6209
    Attachment 6210

    Here is the code

    Java Code:
    System.out.println("RECORED RESULTS \nInv/Mo.      Rate       Years       Future Value");
            valuesProcessor:
            for(String[] row : values)
            {
                int counterVariable = 0;
                for(String col : row)
                {
                    if(col == null)
                        break valuesProcessor;
                    else if(counterVariable == 0)
                        System.out.printf("%s", col);
                    else if(counterVariable == 1)
                        System.out.printf("%10s", col);
                    else if(counterVariable == 2)
                        System.out.printf("%7s", col);
                    else if(counterVariable == 3)
                        System.out.printf("%35s", col);
                    System.out.print(" ");
                    counterVariable++;
                }
                System.out.println();
            }
    Also, how am I suppose to set the width of these columns.

  8. #8
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,391
    Rep Power
    25

    Default Re: How can I make a table nice and neat.

    Post the results here, not links. Wrapping them in code tags should preserve the formatting.
    If you don't understand my response, don't ignore it, ask a question.

  9. #9
    coolman50544 is offline Member
    Join Date
    Apr 2014
    Location
    Miami, FL
    Posts
    47
    Rep Power
    0

    Default Re: How can I make a table nice and neat.

    Ah sorry, I tried to use the insert picture button.
    Java Code:
    FORMATTED RESULTS
    Monthly investment:	$999.00
    Yearly interest rate:	29.0%
    Number of years:	99
    Future value:		$88,518,885,592,137,024.00
    
    Continue? (y/n):
    n
    RECORED RESULTS 
    Inv/Mo.      Rate       Years       Future Value
    $999.00      29.0%      99          $88,518,885,592,137,024.00
    Java Code:
    FORMATTED RESULTS
    Monthly investment:	$324.00
    Yearly interest rate:	29.0%
    Number of years:	9
    Future value:		$167,289.57
    
    Continue? (y/n):
    N
    RECORED RESULTS 
    Inv/Mo.      Rate       Years       Future Value
    $324.00      29.0%       9                         $167,289.57

  10. #10
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,391
    Rep Power
    25

    Default Re: How can I make a table nice and neat.

    Can you explain what is wrong?
    If you don't understand my response, don't ignore it, ask a question.

  11. #11
    coolman50544 is offline Member
    Join Date
    Apr 2014
    Location
    Miami, FL
    Posts
    47
    Rep Power
    0

    Default Re: How can I make a table nice and neat.

    When I enter the values in the first result the table is correctly aligned.
    The second result with different inputs is not correctly aligned, specifically the Years and Future Value.

  12. #12
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,391
    Rep Power
    25

    Default Re: How can I make a table nice and neat.

    Where would "correctly" aligned be? I see the low order 9 in both cases under the e in Year.

    The format Strings can be built using variables. They do NOT have to be hardcoded:
    Java Code:
      int w1 = 10;  // change this to widen the column where Data will print
      System.out.printf("%"+w1+"s\n", "Data");
    If you don't understand my response, don't ignore it, ask a question.

  13. #13
    coolman50544 is offline Member
    Join Date
    Apr 2014
    Location
    Miami, FL
    Posts
    47
    Rep Power
    0

    Default Re: How can I make a table nice and neat.

    Correctly aligned would be like exactly the first result. Also, what does hard coding mean?

  14. #14
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,391
    Rep Power
    25

    Default Re: How can I make a table nice and neat.

    what does hard coding mean?
    Using a literal like 10 or 3 vs using a variable whose value can be changed as needed.
    Java Code:
     System.out.printf("%35s", col);  //  hardcoded 35
    // vs
     int w0 = 35;
     System.out.printf("%"+w0+"s", col); // use a variable
    Correctly aligned would be like exactly the first result.
    I see both the outputs as being aligned. The rightmost digits are in the same column for both.
    What columns do you want to be different?
    If you don't understand my response, don't ignore it, ask a question.

  15. #15
    coolman50544 is offline Member
    Join Date
    Apr 2014
    Location
    Miami, FL
    Posts
    47
    Rep Power
    0

    Default Re: How can I make a table nice and neat.

    I want the first digit of the year value to be directly under the Y in years and futureValue is way off to the right in the second result. I want the first digit of the FutureValue value to be directly under the F in FutureValue

  16. #16
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,391
    Rep Power
    25

    Default Re: How can I make a table nice and neat.

    first digit of the FutureValue value to be directly under the F in FutureValue
    That means the width of the column where the value is shown must be calculated for each value instead of hardcoding a width.
    Use a paper and pencil to draw different values to get the formula to compute width needed.
    If you don't understand my response, don't ignore it, ask a question.

Similar Threads

  1. Neat Metal GUI
    By anthropamorphic in forum Reviews / Advertising
    Replies: 1
    Last Post: 08-08-2013, 03:50 AM
  2. Replies: 25
    Last Post: 11-06-2012, 02:51 AM
  3. Is there a neat way of storing program files?
    By ozzyman in forum New To Java
    Replies: 5
    Last Post: 04-01-2011, 02:21 PM
  4. ANT (Another Neat Tool) - II
    By Java Tutorial in forum Java Tutorial
    Replies: 0
    Last Post: 05-24-2008, 02:44 PM
  5. ANT (Another Neat Tool)
    By Java Tutorial in forum Java Tutorial
    Replies: 0
    Last Post: 05-17-2008, 11:42 AM

Tags for this Thread

Posting Permissions

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