• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Need help with right align for number pyramid

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have written this code for the number pyramid:



I get this:
1
22
333
4444
55555

What I need is this: (Edit, sorry, the spacing doesn't come out properly). It should be same output as above, but everything justified on the right instead of left.

How do I get the output to line up on the right side?

Thank you for your time.

[edit - added code tags]
 
lowercase baba
Posts: 13091
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
you have a counter that tells you how many times to print out the current digit.

What you need to do is first print the space character some number of times...

(that's a hint)
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
take a look at org.apache.commons.lang.StringUtils method rightPad, you have to include jakarta commons (commons-lang.jar, visit http://commons.apache.org/)

http://commons.apache.org/lang//api/org/apache/commons/lang/StringUtils.html
 
Steve Paj
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you for the comments/hints. Still working on it, but not with much success.
 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Steve Paj wrote:I have written this code for the number pyramid:





Since you're trying to display a number pyramid, I figured I might as well help you display a pyramid:



It's a small change to the code:



Hope that helps. You can change the k-space-loop depending on how big your console is. I tried it on a 80x25 shell window.
 
Steve Paj
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you for the information. This gave me an actual pyramid, but still not quite what I needed. I will work with this example and see if I can make it work.
 
Steve Paj
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Using the example Manu Ullas provided, I finally figured it out. Thanks to all for the help.

 
Marshal
Posts: 80497
455
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please always use the Code button. I have edited your last post.
 
Steve Paj
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you very much. I was wondering how the heck you guys got the code to look so neat. I didn't even realize there was a button up there. I will definitely use it in the future.
reply
    Bookmark Topic Watch Topic
  • New Topic