• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
 
Ranch Hand
Posts: 8945
Firefox Browser Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
May be this will help
http://java.sun.com/j2se/1.3/docs/api/java/text/package-summary.html
 
Ranch Hand
Posts: 280
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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,
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
 
xin wen
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks all for replying and helps!
I will try to see if I can make it working!
 
Ranch Hand
Posts: 348
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
some otehr convenient methods regarding alignment I saw at Javaranch
(sorry, forgot who the author is )
 
Ranch Hand
Posts: 218
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Using StringBuffer will be a better approach.
-Sainudheen
 
reply
    Bookmark Topic Watch Topic
  • New Topic