File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
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 The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Printing tab characters" Watch "Printing tab characters" New topic
Author

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

M\t\tVIRSA_CC_GENOBJ

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

M VIRSA_CC_GENOBJ


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
Greenhorn

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
Sheriff

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
sscce.org
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Printing tab characters