Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
The moose likes Beginning Java and the fly likes print out spaces Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "print out spaces" Watch "print out spaces" New topic

print out spaces

Martyn Clark
Ranch Hand

Joined: Apr 16, 2005
Posts: 108
i have an array of objects only when i print to the screen if any of the names have 6 or more charcters in it when it prints out, all those over 6 characters make the next colum move to the right, so they are not all nested neatly under each others. Does any one have any idias how to solve this?

Martyn...<br /> <br />SCJP 1.4 SCWCD 1.4
Tempora Telora
Ranch Hand

Joined: Jun 20, 2005
Posts: 83
post your code please.
Martyn Clark
Ranch Hand

Joined: Apr 16, 2005
Posts: 108
hi here is the code for print out:

System.out.println("Student name\tStudent course");
for (int i = 0; i < store.getSize(); i++) {
System.out.println(i + 1 + store.retrieveList(i).getName()+ "\t\t" + store.retrieveList(i).getCourse());
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
The tab character spaces over to the next multiple of 8 plus 1. So a short name spaces over to 9, a longer name spaces over to 17. (Some environments use values other than 8, eg your editor might use 4.)

I'd probably solve this by picking a length to use for all names and manually padding with spaces. Something like this will do:

name = (name + TEN_SPACES).substring(0, 10);
println( name );

The second or third time I had to do it, I might want to make a set of generic padding methods:

println( leftJustify(name, 10) + rightJustify(size, 5) );

I based my methods on the REXX language, which has very nice syntax. Google for "rexx left" or "rexx right" for examples.
[ June 29, 2005: Message edited by: Stan James ]

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
Martyn Clark
Ranch Hand

Joined: Apr 16, 2005
Posts: 108
Thanks for that stan, i will do a search and check it out.
I agree. Here's the link:
subject: print out spaces
It's not a secret anymore!