Results 1 to 6 of 6
  1. #1
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default String formatting

    I am very unfamiliar with string formatting via printf/format and I am running into a small problem, that may not be quite worth a post and I apologize if it is not, but I am trying to specify the length of a string width with a variable and I'm not sure how.

    the code:
    Java Code:
    f.format("%-15s %5s %10.2f\n", "Tax", "", total * 0.06);
    I want to be able to declare a variable for the width in the two strings so that I can easily change one variable to change the format instead of changing 6 nearly identical lines of code. I only gave this bit of code but f.format is a formatter going to System.out.

    I tried using the arg index modifier and replacing it to
    Java Code:
    private int left = 15;
    private int mid = 5;
    f.format("%-2$s, %4s, %10.2f\n" "Tax", left, "", mid, total * 0.06);
    this didn't work, I also tried:
    Java Code:
    f.format("%-%ds, %d%s, %10.2f\n" left, "Tax", mid, "", total * 0.06);
    which did not work. Like I said I am new to using format and I'm still getting a grasp on the syntax, thanks in advance for any help!

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

    Default

    I'm not sure if it's what you are after, but you can use a formatted string as a format.

    Java Code:
    public class Example {
        public static void main(String[] args) {
            test(3);
            test(4);
            test(5);
        }
    
            /** Prints pi to a given number of dp. */
        private static void test(int prec) {
            System.out.printf(String.format("%%.%df%%n", prec), Math.PI);
        }
    }

    (might be better as two statements: one to create the format string, and one to use it)

  3. #3
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    I was able to format it how I wanted with this
    Java Code:
    f.format(String.format("%%-%ds", left), "Tax");
    f.format(String.format("%%%ds", mid), "");
    f.format(String.format("%%.%df",  far), total * 0.06);
    my problem is that in order to get the middle item to look the same as the result from
    Java Code:
    f.format("%-15s %5s %10.2f\n", "Tax", "", total * 0.06);
    I need to set the mid variable to 12 and I can't quite understand why.

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

    Default

    I need to set the mid variable to 12 and I can't quite understand why.

    How big you have to make mid will depend on the value of total. Remember that in the second form you are right aligning the value of total*0.06 within a space 10 characters wide.

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

    Default

    I'm still not really sure about what you're trying to do. But notice the following two lines do exactly the same thing:

    Java Code:
    f.format("%-15s %5s %10.2f\n", "Tax", "", total * 0.06);
    f.format("%-15s%17.2f\n", "Tax", total * 0.06);

    So if you want the width of the left hand label and the precision of the right hand number to be variable you can get a third equivalent with

    Java Code:
    int left = 15;
    int prec = 2;
    String fmt = String.format("%%-%ds%%17.%df%%n", left, prec);
    f.format(fmt, "Tax", total * 0.06);

    (which avoids mid altogether)

  6. #6
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Sorry if I was a bit confusing, I believe I got it all figured out correctly, Im sure I will improve with practice using format, thanks for the help!

Similar Threads

  1. Replies: 2
    Last Post: 07-02-2010, 03:20 PM
  2. Formatting String data from database
    By computerbum in forum JDBC
    Replies: 1
    Last Post: 04-28-2010, 02:27 PM
  3. String and number formatting
    By computerbum in forum New To Java
    Replies: 1
    Last Post: 04-27-2010, 06:06 PM
  4. Formatting String
    By ersachinjain in forum Advanced Java
    Replies: 13
    Last Post: 10-21-2009, 10:26 PM
  5. formatting String
    By bugger in forum New To Java
    Replies: 1
    Last Post: 11-16-2007, 08:27 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
  •