aspose file tools*
The moose likes Beginning Java and the fly likes format a pattern output Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "format a pattern output" Watch "format a pattern output" New topic
Author

format a pattern output

Roger Fed
Ranch Hand

Joined: Oct 17, 2010
Posts: 82

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..............






I Hear I Forget, I See I Remember, I Do I Understand.
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11480
    
  16

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'.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Roger Fed
Ranch Hand

Joined: Oct 17, 2010
Posts: 82

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

Joined: Oct 02, 2003
Posts: 11480
    
  16

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?
 
 
subject: format a pattern output