Win a copy of TDD for a Shopping Website LiveProject this week in the Testing forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Printing tab characters

 
Ranch Hand
Posts: 413
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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




 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
tab characters are ASCII characters (ASCII 0x09), not Strings, try using Java's Formatter class through String.format.

 
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can you post the code that causes the wrong output?
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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...?
 
Try 100 things. 2 will work out, but you will never know in advance which 2. This tiny ad might be one:
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic