• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

print out spaces

 
Martyn Clark
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,
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?
 
Tempora Telora
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
post your code please.
 
Martyn Clark
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi here is the code for print out:

System.out.println("Student name\tStudent course");
System.out.println("------------\t--------------");
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
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Martyn Clark
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for that stan, i will do a search and check it out.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic