• 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:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

format a pattern output

 
Ranch Hand
Posts: 82
Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hello all, I wrote a simple program that display a pattern of numbers

You can see the output through running as it has spaces before numbers and the page doesn't allow spaces to be
displayed before numbers.
well, all i need in this program is that i want to insert space between numbers
and the pattern remain as it is.
I'd be grateful if you give me any notes about my program
thanks..............





 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
so...after you print a number, print a space.

Note that since each 'number' will now take two positions (one for the digit, and one for the space) you may need to print two spaces instead of one on your line 11, depending on what you want the actual output to look like.

also, this will have problems if your '6' changes to something larger than '9'.
 
Roger Fed
Ranch Hand
Posts: 82
Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thank you very much, you really helped me
but is there any way to fix the problem of numbers greater than 9
 
fred rosenberger
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
sure...but it's not trivial.

part of the problem is that you have to know how much space you need before you print your first line. I.e., if the biggest number is one digit, you only need one space. if the biggest is 2 digits, you need two spaces for the first few numbers, then only one.

if your biggest number is 3 digits, you'll need to pad three spaces for the first few, then two spaces for the next 90, then 1 space...

etc.

Of course, I'm just guessing here, since I don't know what you are REALLY trying to do. What is your actual goal/what do your actual specs say?
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic