[prog] Java printing

Laurel Fan laurel at sdf.lonestar.org
Fri Oct 18 12:50:21 EST 2002


On Fri, Oct 18, 2002 at 10:35:01PM +1000, Sue Stones wrote:
> I am having trouble with a java program not doing what I think it
> should.  So in order to debug it I would like to print out codes for
> non-visisble characters ( \n,\r,\t etc).  I have not found any
> documentation for System.out.println, nor any other way of doing
> this.  Does anyone know how to print anything that will tell me
> which if any of these caracters is being passed around.

I can't think if any library function offhand, and haven't looked in
the docs, assuming that if you haven't found anything, I probably
wouldn't.

If it was just for debugging, I'd hack up something like:

void printEscapedString(String s)
{
    for(int i = 0; i < s.length(); s++) {
        if (s[i] == '\n') {         // if it's something we're looking for 
            print("\\n");           // print the escaped character
        } else if (s[i] == '\r') {  // (the extra \ is so it actually prints
            print("\\r");           // "\n" instead of a newline.) 
        } else if (s[i] == '\t') {  // of course, instead of print(), use
            print("\\t");           // the appropriate function in java
        } else {
            print(s[i]);            // otherwise just print the character
        }
    }
}


-- 
laurel at sdf.lonestar.org
http://dreadnought.gorgorg.org



More information about the Programming mailing list