Win a copy of Grokking Bitcoin this week in the Cloud/Virtualization 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
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Frits Walraven
Bartenders:
  • Carey Brown
  • salvin francis
  • Claude Moore

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: 12738
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: 64137
215
  • 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.
 
I love a good mentalist. And so does this tiny ad:
Create Edit Print & Convert PDF Using Free API with Java
https://coderanch.com/wiki/703735/Create-Convert-PDF-Free-Spire
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!