This week's book giveaway is in the Python forum.
We're giving away four copies of Python Continuous Integration and Delivery and have Moritz Lenz on-line!
See this thread for details.
Win a copy of Python Continuous Integration and Delivery this week in the Python 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
  • Liutauras Vilda
  • Bear Bibeault
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Devaka Cooray
  • Junilu Lacar
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Tim Holloway
  • Claude Moore
  • Stephan van Hulst
Bartenders:
  • Winston Gutkowski
  • Carey Brown
  • Frits Walraven

Need help with right align for number pyramid  RSS feed

 
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]
 
lowercase baba
Posts: 12724
51
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)
 
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.
 
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.

 
Marshal
Posts: 63426
205
  • 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.
 
it's a teeny, tiny, wafer thin ad:
Become a Java guru with IntelliJ IDEA
https://www.jetbrains.com/idea/
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!