Results 1 to 7 of 7
- 07-09-2010, 07:35 AM #1
Member
- Join Date
- Jul 2010
- Posts
- 11
- Rep Power
- 0
Print Formatting in Columns trouble
Having written a program that gets the mouses coordinates and the different RGB values of the pixel at those coordinates I then sought after recording the data in a text file. Ideally for easy viewing later I wanted each category separated into different columns, something like pic1 (smaller image).
Instead, I am getting results like pic2.
In attempt to format the text into columns, i just used tabs, however this does not seem to work properly... here is the code just for the printing (I am outputting to a text file):
I did look at the Formatter class, however I found nothing of use.Java Code:record.println("X: " + X + "\tY: " + Y + "\tR: " + red + "\tG: " + green + "\tB: " + blue);
I do know that in the Turing language you can format a print statement like so: ('put' is the Turing equivalent of 'System.out.println();')
And the output will display as "hello you" because hello is 5 characters long, but you specified the first put (print) statement to be 6 characters long.Java Code:put "hello":6 put "you"
Any ideas on how to solve this problem?
Thanks.
- 07-09-2010, 07:40 AM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,601
- Blog Entries
- 7
- Rep Power
- 17
- 07-09-2010, 08:44 AM #3
Member
- Join Date
- Jul 2010
- Posts
- 11
- Rep Power
- 0
Your absolutely right. I should have read the API docs better, (although it is 3 am...:rolleyes:).
I did look over the right thing, just misinterpreted what it did. I thought that for example printf("%1d",5); would have printed something like " 5" and not just "5".
I got everything working with the following code:
Although in the meantime just after posting the first post in this thread, I did come up with a crude fix for my problem by making a small method to add any necessary spaces...Java Code:record.printf("X: %-5d " + "Y: %-4d " + "R: %-4d " + "G: %-4d " + "B: %-4d\n",X,Y,red,green,blue);
Java Code:public String format(int num, int length) { String f = Integer.toString(num); for (int i = 0; i < length; i++) { try { f.charAt(i); // check to see if there is anything at the current // index position } catch (Exception e) { f = f + " "; // if not, add a space. } } return f; }
- 07-09-2010, 08:55 AM #4
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,601
- Blog Entries
- 7
- Rep Power
- 17
That fix is indeed too crude: Exceptions are thrown each time you are peeking beyond the length of that String f and then you add one single space to it. Better use StringBuilders for that purpose:
This method basically does the same as your version but it is much cheaper in execution time and memory.Java Code:public String format(int num, int length) { StringBuilder sb= new StringBuilder(length); sb.append(num); while (sb.length() < length) sb.append(' '); return sb.substring(0, length); }
kind regards,
Jos
- 07-09-2010, 09:16 AM #5
Member
- Join Date
- Jul 2010
- Posts
- 11
- Rep Power
- 0
yea, I wasn't exactly thinking when I came up with that one... :P
I also found that this worked as well:
I don't know why I didn't think of that one to begin with...Java Code:public static String format2(int num, int length) { String f = Integer.toString(num); while(f.length()<length){ f = f.concat(" "); // would f = f + ' '; be more efficient? } return f; }
Would the above method be any less efficient than using your method with a StringBuilder?
Thanks for all the help.:D
- 07-09-2010, 09:27 AM #6
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,601
- Blog Entries
- 7
- Rep Power
- 17
Adding something to a String involves StringBuffers; the compiler does that when it sees you adding two Strings or a String and something else. It creates a new StringBuffer for each addition; we (the humans) can do better than that (see my first example) and use one StringBuilder for the entire process. The compiler is a bit near sighted so it generates a bit stupid code ...
kind regards,
Jos
- 07-09-2010, 10:23 AM #7
Member
- Join Date
- Jul 2010
- Posts
- 11
- Rep Power
- 0
Similar Threads
-
Date formatting
By bikkerss in forum New To JavaReplies: 13Last Post: 05-08-2010, 04:07 PM -
formatting..
By sireesha in forum New To JavaReplies: 16Last Post: 06-26-2009, 07:11 PM -
Formatting a toString
By MooNinja in forum New To JavaReplies: 8Last Post: 03-31-2009, 07:32 PM -
Print the text file and print preview them
By Java Tip in forum java.awtReplies: 0Last Post: 06-22-2008, 11:04 PM -
Formatting the date
By yuchuang in forum New To JavaReplies: 5Last Post: 05-07-2007, 06:08 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks