Results 1 to 8 of 8
Like Tree1Likes
  • 1 Post By Fubarable

Thread: start position argument for PRINTF()??

  1. #1
    ajetrumpet is offline Member
    Join Date
    Feb 2013
    Posts
    57
    Rep Power
    0

    Default start position argument for PRINTF()??

    Hello all,

    I need to print out a formatted table with subheadings, but apparently printf() doesn't have a position argument? Hopefully I didn't miss it!

    I need left justified placements at 15, 30, 45 and 60 but all I see in the docs are symbolization references for justification (e.g. - either "-" or no neg. sign). Can someone give me a push here?

    thanks!

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: start position argument for PRINTF()??

    You just need to do a little math is all to calculate the width constant for each argument.

  3. #3
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,431
    Blog Entries
    7
    Rep Power
    20

    Default Re: start position argument for PRINTF()??

    You can't just 'jump' to a certain position; you have to use tabs and/or spaces together with the field width and precision specifiers and the justification flags.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  4. #4
    ajetrumpet is offline Member
    Join Date
    Feb 2013
    Posts
    57
    Rep Power
    0

    Default Re: start position argument for PRINTF()??

    sounds good. I got it. Interesting what features the formatter has and what features it doesn't. It's good for a few things, as it seems. Here's the code I used, for the next person that view this in seek of help:

    Java Code:
            System.out.printf("%-20s %-20s%n", heading_1, heading_2); //headings.
            System.out.printf("%-20s %-20s %-20s %-20s%n", "", subHeading_1, subHeading_2, subHeading_3); //subheadings.
            
            System.out.printf("%-20s %-20s %-20s %-20s%n", "str1", 
                              new DecimalFormat("#.#####").format(str1_km), 
                              new DecimalFormat("#.#####").format(str1_m), 
                              new DecimalFormat("#.#####").format(str1_mi)); //str1
    
            System.out.printf("%-20s %-20s %-20s %-20s%n", "str2", 
                              new DecimalFormat("#.#####").format(str2_km), 
                              new DecimalFormat("#.#####").format(str2_m), 
                              new DecimalFormat("#.#####").format(str2_mi)); //str2
                              
            System.out.printf("%-20s %-20s %-20s %-20s%n", "str3", 
                              new DecimalFormat("#.#####").format(str3_km), 
                              new DecimalFormat("#.#####").format(str3_m), 
                              new DecimalFormat("#.#####").format(str3_mi)); //str3
                              
            System.out.printf("%-20s %-20s %-20s %-20s%n", "str4", 
                              new DecimalFormat("#.#####").format(str4_km), 
                              new DecimalFormat("#.#####").format(str4_m), 
                              new DecimalFormat("#.#####").format(str4_mi)); //str4

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: start position argument for PRINTF()??

    I'm not sure why you would need to create many DecimalFormat objects since they'd all do the same thing. The code could be simplified if you just created one DecimalFormat object and used it to format your numbers. It could be simplified even further if you just got rid of the DecimalFormat objects altogether since they seem redundant since you're already using System.out.printf. Why not use the float specifier in your printf rather than the String specifier when dealing with floating point numbers? For example:

    Java Code:
    public class Foo02 {
       public static final String HEADING_FORMAT = 
             "%-20s %-20s%n";
       public static final String SUB_HEADING_FORMAT = 
             "%-20s %-20s %-20s %-20s%n";
       public static final String ROW_FORMAT =
             "%-20s %-20.4f %-20.4f %-20.4f%n";
       
       public static void main(String[] args) {
          
          double[][] data = {
                {1.01020304, 2.01020304, 3.01020304},
                {4.01020304, 5.01020304, 6.01020304},
                {7.01020304, 8.01020304, 9.01020304},
                {1.01020304, 2.01020304, 3.01020304},
                {4.01020304, 5.01020304, 6.01020304},
                {7.01020304, 8.01020304, 9.01020304}
          };
          
          String heading_1 = "heading 1";
          String heading_2 = "heading 2";
          String subHeading_1 = "subHeading 1";
          String subHeading_2 = "subHeading 2";
          String subHeading_3 = "subHeading 3";
          System.out.printf(HEADING_FORMAT, heading_1, heading_2); 
          System.out.printf(SUB_HEADING_FORMAT, "", 
                subHeading_1, subHeading_2, subHeading_3); 
           
          for (int i = 0; i < data.length; i++) {
             System.out.printf(ROW_FORMAT, "str " + (i + 1), 
                   data[i][0], data[i][1], data[i][2]);
          }       
       }
    }

  6. #6
    ajetrumpet is offline Member
    Join Date
    Feb 2013
    Posts
    57
    Rep Power
    0

    Default Re: start position argument for PRINTF()??

    Quote Originally Posted by Fubarable View Post
    I'm not sure why you would need to create many DecimalFormat objects since they'd all do the same thing. The code could be simplified if you just created one DecimalFormat object and used it to format your numbers. It could be simplified even further if you just got rid of the DecimalFormat objects altogether since they seem redundant since you're already using System.out.printf. Why not use the float specifier in your printf rather than the String specifier when dealing with floating point numbers?
    The reason is because I don't have the time to look the simplification up. That's what programmers are for. Patternization works just fine, even though it takes a little more memory. I'm past being a software developer at this point.

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: start position argument for PRINTF()??

    Quote Originally Posted by ajetrumpet View Post
    The reason is because I don't have the time to look the simplification up. That's what programmers are for. Patternization works just fine, even though it takes a little more memory. I'm past being a software developer at this point.
    Well I'm glad you've clarified your thoughts for us. Since you are more interested in getting answers than in learning to program, I suggest that next time you ask your questions on a paid site and not an all volunteer site. We prefer to be thought of as teachers who try to guide students towards understanding how to program in Java rather than as minions who serve greater masters such as yourself.
    DarrylBurke likes this.

  8. #8
    ajetrumpet is offline Member
    Join Date
    Feb 2013
    Posts
    57
    Rep Power
    0

    Default Re: start position argument for PRINTF()??

    Quote Originally Posted by Fubarable View Post
    Well I'm glad you've clarified your thoughts for us. Since you are more interested in getting answers than in learning to program, I suggest that next time you ask your questions on a paid site and not an all volunteer site. We prefer to be thought of as teachers who try to guide students towards understanding how to program in Java rather than as minions who serve greater masters such as yourself.
    That was a joke! Chill, sir. :)

    Actually, FYI, talents can be used much more effectively this way and everyone *can* win in a situation like this. To be honest, it's this very phenomenon that's got the USA's economy into such a deep dark hole at the moment. The leaders are currently the BS'ers when the BS'ers should really be following a bit and learning.

    But at any rate, just wanted to let you know that was a joke. No harm done...have a good one!

    again, thanks for the updated script too! I actually learned something from that, most notably the flexibility of working with Java-based arrays.

Similar Threads

  1. printf VS printWriter.printf
    By Mapisto in forum New To Java
    Replies: 14
    Last Post: 01-20-2012, 12:29 PM
  2. Replies: 1
    Last Post: 01-18-2011, 06:27 PM
  3. printf
    By razzle in forum New To Java
    Replies: 4
    Last Post: 11-12-2010, 02:13 PM
  4. get position in string from caret position
    By helloworld111 in forum AWT / Swing
    Replies: 5
    Last Post: 02-19-2009, 01:36 AM
  5. printf
    By Jack in forum New To Java
    Replies: 2
    Last Post: 07-04-2007, 04:31 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
  •