This week's book giveaway is in the OCPJP forum. We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line! See this thread for details.
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?
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.
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
Joined: Sep 25, 2003
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
Joined: Jan 29, 2003
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.