This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Question on System.out.print(...) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Question on System.out.print(...)" Watch "Question on System.out.print(...)" New topic
Author

Question on System.out.print(...)

xin wen
Ranch Hand

Joined: Oct 11, 2002
Posts: 64
Anyone know if there is a way to do this?
When I use System.out.print(...), there are three columns and twenty rows, but the data is in different size, so when I displayed them, they can not be at the same columns, for example:
abcde adddddd yyyyyyyy
as jk rt
What I want is to adjust jk in adddddd column, and rt is in yyyyyyy column, so I wander if you know how to do it in java?
Thanks so much!
Pradeep bhatt
Ranch Hand

Joined: Feb 27, 2002
Posts: 8903

May be this will help
http://java.sun.com/j2se/1.3/docs/api/java/text/package-summary.html


Groovy
Jason Kretzer
Ranch Hand

Joined: May 31, 2001
Posts: 280
One way, maybe not the best way is to pad with spaces. For example, lets say each column is 20 spaces wide. And you have to display the following row
String[] s1 = {"asdf", "fdsa", "jklh", "hlkj"};
each of which is its own String. You could use a StringBuffer and pad as so

this should give you a rough idea on how it can be done.
Best Regards,


Jason R. Kretzer<br />Software Engineer<br />System Administrator<br /><a href="http://alia.iwarp.com" target="_blank" rel="nofollow">http://alia.iwarp.com</a>
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
Going to std out for screen display, you need to pad the short strings with spaces to match the length of the longest strings. You might have to loop through your data first to find the longest string in column 1 and then pad every row to that length. Or you might know a reasonable maximum length ahead of time.
I made myself a String utility class with static methods inspired by the Rexx language, so I'd do something like:

left and right methods pad or truncate to a given length. You can optionally tell them what pad character to use.
Probably seems like more work than you were hoping for? Hope it helps!


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
xin wen
Ranch Hand

Joined: Oct 11, 2002
Posts: 64
Thanks all for replying and helps!
I will try to see if I can make it working!
chi Lin
Ranch Hand

Joined: Aug 24, 2001
Posts: 348
some otehr convenient methods regarding alignment I saw at Javaranch
(sorry, forgot who the author is )


not so smart guy still curious to learn new stuff every now and then
Sainudheen Mydeen
Ranch Hand

Joined: Aug 18, 2003
Posts: 218
Using StringBuffer will be a better approach.
-Sainudheen
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Question on System.out.print(...)
 
Similar Threads
How do i add a popmenu to a Cell in JTable
JTable
Left most column locking
JTable - Table Model vs Display View Columns
Displaying JTable Column names in the GUI