• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

format a pattern output

 
Roger Fed
Ranch Hand
Posts: 82
Java Oracle
  • Mark post as helpful
  • send pies
  • 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..............





 
fred rosenberger
lowercase baba
Bartender
Posts: 12123
30
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • 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
Java Oracle
  • Mark post as helpful
  • send pies
  • 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
Bartender
Posts: 12123
30
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • 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?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic