Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Need help with right align for number pyramid  RSS feed

 
Steve Paj
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • 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]
 
fred rosenberger
lowercase baba
Bartender
Posts: 12542
48
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • 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)
 
Jose Martín
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
Thank you for the comments/hints. Still working on it, but not with much success.
 
Manu Ullas
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • Quote
  • Report post to moderator
Using the example Manu Ullas provided, I finally figured it out. Thanks to all for the help.

 
Campbell Ritchie
Marshal
Posts: 55717
163
  • Mark post as helpful
  • send pies
  • 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
  • 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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!