• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

string break/flush right

 
sunnie sunshine
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I need help. I'm doing line program and it has a menu with each option performing a different [supposedly] easy action on the actual menu and displaying it again. I have managed most of it, but I'm stuck on making the menu justify right to column 80. I think I may have to break the string or something. Any ideas?
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
See if I got this right. The menu looks like:

Is that what you mean by right justified out to column 80? If not, ignore the rest.
I knew REXX before I knew Java, and REXX has some wonderful string functions that I duplicate in every new language I learn. One called right() right justifies a string to a given length using a given pad character. So

would give you the first menu line above. The right method has a "while" loop while the string is shorter than the specified length, stick the specifed character (or characters!) on the front. The REXX function also truncates the string if it is too long, so a really long menu description would be chopped off rather than mess up the right alignment.
If all that fit your problem, you might want to start your own StringUtil class with reusable methods like right(), left(), center(), etc.
 
sunnie sunshine
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No the menu looks like this
1. blah blah blah
2. blah blah blah
3. blah blah blah blah
etc
and when it justifies right it's supposed to look like this
1. blah blah blah
2. blah blah blah
3. blah blah blah blah
etc
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
HTML ate up your redundant blanks, but I think you meant normal text, right justified, just like doing right just in MS Word. (Use the CODE tag (see buttons below) to make a fixed-font section like this for such examples.)

which is actually a little simpler. Now it's just:

Of course, you still have to write right (right?) to make it pad the string with the space character.
For one-off special cases, you can also use:

It has the appeal of not calling another class that does looping and concatenating, but is not really reusable for other sizes and pad chars.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic