This week's book giveaway is in the OCAJP forum.
We're giving away four copies of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) and have Khalid A Mughal & Rolf W Rasmussen on-line!
See this thread for details.
Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Formatting string for printing

 
vivek ja
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am trying to print out something using my java program.
I was the printed page to be 2 colums.

For this, I need some mechanism where i specify that it should print in 2 columns and it has to print in the first column and continue in the second

I dont have any idea how this can be done, can someone pls guide me.
Thank you!
 
Stefan Wagner
Ranch Hand
Posts: 1923
Linux Postgres Database Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Print to screen/ file?
this will work for foo not being longer than 7 characters:
System.out.println (foo + "\ŧ" + bar);
If you calculate the longest value for the first column, you may fill the right number of tabs to get two columns.

If you print to the printer, you need to specify coordinates allthough, don't you?
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What version of Java are you using? If you are using JDK 1.5, then there is a PrintWriter class that provides a printf() method that behaves similarly to the printf() function from C/C++. It allows for some very sophisticated formatting. You should look at the Javadocs for more information.

Layne
 
vivek ja
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually I have a faxing f unctionality.
I am using a third party tool that will fax whatever string I am sending.
So my string has to be formatted before calling that fax API.

I am trying to format it using the \n and \t characters wherever needed.
The foo+\t+bar adds tabs, but how can I specify that I want 2 columns?
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by vivek ja:
Actually I have a faxing f unctionality.
I am using a third party tool that will fax whatever string I am sending.
So my string has to be formatted before calling that fax API.

I am trying to format it using the \n and \t characters wherever needed.
The foo+\t+bar adds tabs, but how can I specify that I want 2 columns?


Since you probably need to format to a String, you can wrap a StringWriter with the PrintWriter I mentioned before. Whether you do this or use explicit tab characters, there is no way to explicitly state there are two columns. As long as the contents of each column have a limited width, either using PrintWriter or \t characters should work.

Layne
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic