Results 1 to 7 of 7
  1. #1
    Inventor22 is offline Member
    Join Date
    Jul 2010
    Posts
    11
    Rep Power
    0

    Question 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):

    Java Code:
    	    record.println("X: " + X
    		    + "\tY: " + Y
    		    + "\tR: " + red
    		    + "\tG: " + green
    		    + "\tB: " + blue);
    I did look at the Formatter class, however I found nothing of use.

    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();')
    Java Code:
    put "hello":6
    put "you"
    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.

    Any ideas on how to solve this problem?
    Thanks.
    Attached Thumbnails Attached Thumbnails Print Formatting in Columns trouble-pic1.png   Print Formatting in Columns trouble-pic2.png  

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    12,999
    Blog Entries
    7
    Rep Power
    19

    Default

    Quote Originally Posted by Inventor22 View Post
    I did look at the Formatter class, however I found nothing of use.
    Read the API documentation, don't just look at it; even better: read the API documentation for the System.printf( ... ) method; it leads you to that same Formatter class which has the answer.

    kind regards,

    Jos

  3. #3
    Inventor22 is offline Member
    Join Date
    Jul 2010
    Posts
    11
    Rep Power
    0

    Default

    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:

    Java Code:
    	    record.printf("X: %-5d " +
    		    "Y: %-4d " +
    		    "R: %-4d " +
    		    "G: %-4d " +
    		    "B: %-4d\n",X,Y,red,green,blue);
    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:
        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;
        }

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    12,999
    Blog Entries
    7
    Rep Power
    19

    Default

    Quote Originally Posted by Inventor22 View Post
    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:

    Java Code:
    	    record.printf("X: %-5d " +
    		    "Y: %-4d " +
    		    "R: %-4d " +
    		    "G: %-4d " +
    		    "B: %-4d\n",X,Y,red,green,blue);
    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:
        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;
        }
    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:

    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);
    }
    This method basically does the same as your version but it is much cheaper in execution time and memory.

    kind regards,

    Jos

  5. #5
    Inventor22 is offline Member
    Join Date
    Jul 2010
    Posts
    11
    Rep Power
    0

    Default

    yea, I wasn't exactly thinking when I came up with that one... :P

    I also found that this worked as well:

    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;
        }
    I don't know why I didn't think of that one to begin with...
    Would the above method be any less efficient than using your method with a StringBuilder?

    Thanks for all the help.:D

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    12,999
    Blog Entries
    7
    Rep Power
    19

    Default

    Quote Originally Posted by Inventor22 View Post
    yea, I wasn't exactly thinking when I came up with that one... :P

    I also found that this worked as well:

    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;
        }
    I don't know why I didn't think of that one to begin with...
    Would the above method be any less efficient than using your method with a StringBuilder?

    Thanks for all the help.:D
    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

  7. #7
    Inventor22 is offline Member
    Join Date
    Jul 2010
    Posts
    11
    Rep Power
    0

Similar Threads

  1. Date formatting
    By bikkerss in forum New To Java
    Replies: 13
    Last Post: 05-08-2010, 04:07 PM
  2. formatting..
    By sireesha in forum New To Java
    Replies: 16
    Last Post: 06-26-2009, 07:11 PM
  3. Formatting a toString
    By MooNinja in forum New To Java
    Replies: 8
    Last Post: 03-31-2009, 07:32 PM
  4. Print the text file and print preview them
    By Java Tip in forum java.awt
    Replies: 0
    Last Post: 06-22-2008, 11:04 PM
  5. Formatting the date
    By yuchuang in forum New To Java
    Replies: 5
    Last Post: 05-07-2007, 06:08 PM

Tags for this Thread

Posting Permissions

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