This week's book giveaway is in the Server-Side JavaScript and NodeJS forum.
We're giving away four copies of Node.js Design Patterns: Design and implement production-grade Node.js applications using proven patterns and techniques and have Mario Casciaro & Luciano Mammino on-line!
See this thread for details.
Win a copy of Node.js Design Patterns: Design and implement production-grade Node.js applications using proven patterns and techniques this week in the Server-Side JavaScript and NodeJS 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
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

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: 8944
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
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic