Results 1 to 3 of 3
  1. #1
    jwl
    jwl is offline Member
    Join Date
    Jul 2012
    Posts
    93
    Rep Power
    0

    Default formating output

    OK, I have a program that has an output that looks like this:
    Java Code:
    (105, 111) (120, 16) (149, 62)
    (26, 79) (10, 45) (53, 100)
    (98, 118) (44, 73) (70, 116)
    (92, 98) (9, 138) (137, 132)
    (116, 115) (2, 108) (35, 104)
    What I need it to do is format with the last closing bracket of each line to be directly above the one below it; for example:
    Java Code:
    (105, 111) (120, 16) (149, 62 )
    ( 26, 79 )  ( 10, 45 ) ( 53, 100)
    ( 98, 118) ( 44, 73 ) ( 70, 116)
    ( 92, 98 ) (  9, 138) (137, 132)
    (116, 115) (  2, 108) ( 35, 104)
    here is what I have so far in a pringtf String

    Java Code:
    out.printf("(" + x1 + ", " + y1 + ") " + "(" + x2 + ", " + y2
                                  + ") " + "(" + x3 + ", " + y3 + ")");
    It is out.printf because I'm putting it in a file and I am also looping it 5 times which is why there are five different lines.
    Last edited by pbrockway2; 10-13-2012 at 01:33 AM. Reason: code tags added

  2. #2
    jwl
    jwl is offline Member
    Join Date
    Jul 2012
    Posts
    93
    Rep Power
    0

    Default Re: formating output

    The answer
    Java Code:
    out.printf("(%3d, %3d) (%3d, %3d) (%3d, %3d)", x1, y1, x2, y2,
                 x3, y3 );

  3. #3
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default Re: formating output

    I've added code tags to the example output - they're about the nearest thing we have to <pre> here I think, and are useful when formatting matters.

    Ignoring the typo in the second line you seem to be right aligning the first number of the pair and left aligning the second. In code that would look like

    Java Code:
    "(%3d, %-3d) (%3d, %-3d) (%3d, %-3d)%n"
    Using - for alignment and %n for the newline is discussed in the API docs.

    (Or it may be that what you came up with is completely satisfactory - I'm mostly posting in case someone later finds the API docs useful.)

Similar Threads

  1. Replies: 9
    Last Post: 05-14-2012, 12:09 PM
  2. Replies: 2
    Last Post: 05-03-2012, 12:39 PM
  3. Replies: 0
    Last Post: 09-30-2010, 09:16 PM
  4. Problem with formating comments
    By Lubomirus in forum Eclipse
    Replies: 0
    Last Post: 07-07-2009, 09:37 AM
  5. Java, output string, getting correct output? HELP!
    By computerboyo in forum New To Java
    Replies: 2
    Last Post: 02-26-2009, 12:44 AM

Posting Permissions

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