File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes Printing tab characters Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Head First Android this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Printing tab characters" Watch "Printing tab characters" New topic

Printing tab characters

Gaurav Chikara
Ranch Hand

Joined: Jun 09, 2000
Posts: 410
I have tab characters stored as delemitors in a string like

String str = "\t\t";

When I use Printstream and try to print tab .My intention is to created tab seperations in file but
when i see file comes up like this


But when I use ps.write("\t\t"); (Putting value directly in printstream
My results are correct as expected


Is there a way we can get the first case working ?

Thanks in advance

SCJP,SCWCD,SCBCD<br />If Opportunity doesn't knock then build the door
Mos Jeff

Joined: Sep 15, 2010
Posts: 14
tab characters are ASCII characters (ASCII 0x09), not Strings, try using Java's Formatter class through String.format.

Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

Can you post the code that causes the wrong output?

"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
marc weber

Joined: Aug 31, 2004
Posts: 11343

Gaurav Chikara wrote:...when I use ps.write("\t\t"); ... results are correct as expected...

I'm a little confused about how you're successfully using ps.write("\t\t"), because PrintStream's write method using a String parameter is private.

The public write methods take an int or a byte[]. Regarding these, the API says, "Note that the bytes will be written as given; to write characters that will be translated according to the platform's default character encoding, use the print(char) or println(char) methods."

So can you use a print method instead?

The following works for me (using the print method with a String parameter)...

Or am I missing something obvious...?

"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
subject: Printing tab characters
It's not a secret anymore!