Results 1 to 8 of 8
- 02-24-2013, 06:46 AM #1
Member
- Join Date
- Feb 2013
- Posts
- 57
- Rep Power
- 0
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!
-
Re: start position argument for PRINTF()??
You just need to do a little math is all to calculate the width constant for each argument.
- 02-24-2013, 07:27 AM #3
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,385
- Blog Entries
- 7
- Rep Power
- 17
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,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 02-24-2013, 08:58 AM #4
Member
- Join Date
- Feb 2013
- Posts
- 57
- Rep Power
- 0
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
-
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]); } } }
- 02-24-2013, 05:12 PM #6
Member
- Join Date
- Feb 2013
- Posts
- 57
- Rep Power
- 0
Re: start position argument for PRINTF()??
-
Re: start position argument for PRINTF()??
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.
- 02-24-2013, 05:25 PM #8
Member
- Join Date
- Feb 2013
- Posts
- 57
- Rep Power
- 0
Re: start position argument for PRINTF()??
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
-
printf VS printWriter.printf
By Mapisto in forum New To JavaReplies: 14Last Post: 01-20-2012, 12:29 PM -
Start Swing GUI program by Java Web Start with IE in Eclipse debug mode
By albertkao in forum EclipseReplies: 1Last Post: 01-18-2011, 06:27 PM -
printf
By razzle in forum New To JavaReplies: 4Last Post: 11-12-2010, 02:13 PM -
get position in string from caret position
By helloworld111 in forum AWT / SwingReplies: 5Last Post: 02-19-2009, 01:36 AM -
printf
By Jack in forum New To JavaReplies: 2Last Post: 07-04-2007, 04:31 AM


1Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks