Results 1 to 19 of 19
  1. #1
    Rahim2312 is offline Member
    Join Date
    Apr 2012
    Posts
    88
    Rep Power
    0

    Default Formatting Output

    I want to display my output in a tabular format...I need the formatting to read like this:
    Name Grade Age
    Richard 10 17
    Sally 11 18

    And so on and so forth...
    I have tried using:
    Java Code:
    JOptionPane.showMessageDialog("Name" + " \t " + "Grade" + " \t " + "Age" + " \t" );
    but that formatting doesn't exactly fit what I am wanting. Is there a way to set the tab to be a certain distance? I could probably enter two tabs between each of those, but I feel like that is just creating more coding than needed?

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,013
    Rep Power
    10

    Default Re: Formatting Output

    I'm not very clear on what you're looking for. Why exactly doesn't the approach using single tabs work? You say it doesn't exactly fit what you want, but what DOES fit what you want, exactly?
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Rahim2312 is offline Member
    Join Date
    Apr 2012
    Posts
    88
    Rep Power
    0

    Default Re: Formatting Output

    I just realized that for some reason the forum formatted my 1st post. Let me try it like this
    Name..........................Grade............... .............Age


    Well the forum keeps removing my tabs so I added "." I basically want there to be two tabs between the output./
    Last edited by Rahim2312; 05-02-2012 at 06:33 PM.

  4. #4
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,013
    Rep Power
    10

    Default Re: Formatting Output

    Quote Originally Posted by Rahim2312 View Post
    I just realized that for some reason the forum formatted my 1st post. Let me try it like this
    Name..........................Grade............... .............Age


    Well the forum keeps removing my tabs so I added "." I basically want there to be two tabs between the output./
    To preserve formatting, use the code tags.

    But if you want two tabs, just use two tabs. Otherwise use a gui with a layout, or custom painting.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5
    Rahim2312 is offline Member
    Join Date
    Apr 2012
    Posts
    88
    Rep Power
    0

    Default Re: Formatting Output

    How would I use two tabs?

  6. #6
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,013
    Rep Power
    10

    Default Re: Formatting Output

    Quote Originally Posted by Rahim2312 View Post
    How would I use two tabs?
    What have you tried? Part of coding is experimentation.

    You do it the same way you use two of anything. If I want to print out one dot, I do this:

    System.out.prinltn(".");

    Then if I want to print out two dots, I do this:

    System.out.println("..");

    Same thing applies to tabs, newlines, whatever.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  7. #7
    Rahim2312 is offline Member
    Join Date
    Apr 2012
    Posts
    88
    Rep Power
    0

    Default Re: Formatting Output

    I tried this:
    Java Code:
    JOptionPane.showMessageDialog(frame, "Name" + " \t " + "\t" + "Grade" + " \t " + "\t" + "Age" );
    But that isn't allowing the data that pulls in below it to line up correctly based on the lenghts of each

  8. #8
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,013
    Rep Power
    10

    Default Re: Formatting Output

    Quote Originally Posted by Rahim2312 View Post
    I tried this:
    Java Code:
    JOptionPane.showMessageDialog(frame, "Name" + " \t " + "\t" + "Grade" + " \t " + "\t" + "Age" );
    But that isn't allowing the data that pulls in below it to line up correctly based on the lenghts of each
    Note that "\t\t" is the same as "\t" + "\t".

    So you're saying that the lengths of the data throw off the formatting? So why not format the lengths of the data? But if you want to get more complicated than this, your best bet is to use a layout, or a JTable.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  9. #9
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,172
    Rep Power
    20

    Default Re: Formatting Output

    If you really need this to be aligned then, presuming you are only going to view this in a monospaced font, you might have to look at some sort of String.format() thing.
    There are ways in that of defining padding (at least there were in the old C stdlib versions on which this is based). It won't be tabs, but spaces.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  10. #10
    Rahim2312 is offline Member
    Join Date
    Apr 2012
    Posts
    88
    Rep Power
    0

    Default Re: Formatting Output

    Quote Originally Posted by Tolls View Post
    If you really need this to be aligned then, presuming you are only going to view this in a monospaced font, you might have to look at some sort of String.format() thing.
    There are ways in that of defining padding (at least there were in the old C stdlib versions on which this is based). It won't be tabs, but spaces.
    It's only going to be viewed in the JOptionPane. How could I use the String.Format() to accommodate that?

  11. #11
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,013
    Rep Power
    10

    Default Re: Formatting Output

    Quote Originally Posted by Rahim2312 View Post
    It's only going to be viewed in the JOptionPane. How could I use the String.Format() to accommodate that?
    What have you tried?
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  12. #12
    Rahim2312 is offline Member
    Join Date
    Apr 2012
    Posts
    88
    Rep Power
    0

    Default Re: Formatting Output

    I tried this:
    Java Code:
    String string = "first: %s, second: %s";
    String formatted = String.format(string, "one", "two");
    System.out.println(formatted); // first: one, second: two
    But I don't think that really fit the scenario. Is there an easy way to put Java into a "Tabular Output"
    Last edited by Rahim2312; 05-03-2012 at 03:58 AM.

  13. #13
    Rahim2312 is offline Member
    Join Date
    Apr 2012
    Posts
    88
    Rep Power
    0

    Default Re: Formatting Output

    Quote Originally Posted by KevinWorkman View Post
    Note that "\t\t" is the same as "\t" + "\t".
    BTW Thank you for that tip! I did not know that :)

  14. #14
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,172
    Rep Power
    20

    Default Re: Formatting Output

    Quote Originally Posted by Rahim2312 View Post
    I tried this:
    Java Code:
    String string = "first: %s, second: %s";
    String formatted = String.format(string, "one", "two");
    System.out.println(formatted); // first: one, second: two
    But I don't think that really fit the scenario.
    It would if you had read the docs on formatting (which are linked from the API for the format() method).
    Give the second %s a width, which will pad spaces to the left (eg %20s).

    Quote Originally Posted by Rahim2312 View Post
    Is there an easy way to put Java into a "Tabular Output"
    Yes, use a JTable.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  15. #15
    Rahim2312 is offline Member
    Join Date
    Apr 2012
    Posts
    88
    Rep Power
    0

    Default Re: Formatting Output

    Now I am on the right track...i will have to toy with this code and see if I can get this to work exactly as I need. Thank you for the help (I may be back :) )

  16. #16
    Rahim2312 is offline Member
    Join Date
    Apr 2012
    Posts
    88
    Rep Power
    0

    Default Re: Formatting Output

    Quote Originally Posted by Tolls View Post
    It would if you had read the docs on formatting (which are linked from the API for the format() method).
    Give the second %s a width, which will pad spaces to the left (eg %20s).
    Is there a way to combine two variables to display as the "first"

  17. #17
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,172
    Rep Power
    20

    Default Re: Formatting Output

    Assuming you mean you want two variables together then pad them, you'd probably need to concatenate them first then apply them to the final format.
    So something like:
    Java Code:
    String concat = String.format("%s %s", someVar, someOtherVar);
    String final = String.format("%s%20s%20s", concat, third, fourth);
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  18. #18
    Rahim2312 is offline Member
    Join Date
    Apr 2012
    Posts
    88
    Rep Power
    0

    Default Re: Formatting Output

    Quote Originally Posted by Tolls View Post
    Assuming you mean you want two variables together then pad them, you'd probably need to concatenate them first then apply them to the final format.
    So something like:
    Java Code:
    String concat = String.format("%s %s", someVar, someOtherVar);
    String final = String.format("%s%20s%20s", concat, third, fourth);
    In your above code, I am assuming using concat is saying to concatenate the two variables listed above, is that correct?

    Java Code:
    		String concat = String.format("%s %s", input1, input2);
    		String final = String.format("%s%20s%20s", concat, total);
    		System.out.prtinln(final);
    I tried using that, and it produced numerous javac errors.
    Last edited by Rahim2312; 05-03-2012 at 09:07 PM. Reason: Forgot code tags

  19. #19
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,172
    Rep Power
    20

    Default Re: Formatting Output

    Sorry, my fault.
    'final' is a keyword...:)
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. formatting data output
    By droidus in forum New To Java
    Replies: 16
    Last Post: 04-13-2011, 12:26 AM
  2. Help with formatting output
    By John Lord in forum New To Java
    Replies: 1
    Last Post: 10-31-2010, 01:10 PM
  3. formatting numbers in output
    By andy3 in forum New To Java
    Replies: 3
    Last Post: 05-27-2010, 07:43 PM
  4. Formatting output somewhat like a table
    By latereg in forum New To Java
    Replies: 3
    Last Post: 04-06-2010, 07:44 AM
  5. Need help formatting File Output
    By aaroncarpet in forum New To Java
    Replies: 2
    Last Post: 11-26-2009, 06:26 PM

Posting Permissions

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