This week's book giveaway is in the Server-Side JavaScript and NodeJS forum.
We're giving away four copies of Micro Frontends in Action and have Michael Geers on-line!
See this thread for details.
Win a copy of Micro Frontends in Action 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

string break/flush right

 
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?
 
(instanceof Sidekick)
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)
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.
 
She's brilliant. She can see what can be and is not limited to what is. And she knows 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