aspose file tools*
The moose likes Beginning Java and the fly likes Problem with spacing Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Problem with spacing" Watch "Problem with spacing" New topic
Author

Problem with spacing

James.Seung Lee
Greenhorn

Joined: Sep 30, 2011
Posts: 2
Hi, I'm trying to output figures of Christmas trees using a method that accepts two parameters, segment and height. I just can't get the spacing right!!! They should be centered, and I can only use very basic level Java. Thank you so much for any help!!

public class ChristmasTrees
{
public static void main(String[] args)
{
drawTree(3, 4);
System.out.println();
drawTree(2, 5);
}

public static void drawTree(int segments, int height)
{
for (int i = 1; i <= segments; i++)
{
for (int line = 1; line <= height; line++)
{
for (int spaces = 0; spaces <= (height - line); spaces++)
{
System.out.print(" ");
}

for (int dots = 1; dots <= (2*line - 1) + 2*(i-1); dots++)
{
System.out.print("*");
}
System.out.println();
}
}
System.out.println(" * ");
System.out.println(" * ");
System.out.println(" ******* ");
}
}
Ralph Cook
Ranch Hand

Joined: May 29, 2005
Posts: 479
I think you have one too many loops. The outermost loop is once per "segment" (I'm not sure what that means), and the one just inside that is for "lines". The loop for lines starts off giving the line with 1 star, then the line with 3, and so forth through the 4 lines of the tree, and works correctly as near as I can tell. But then your "segment" loop starts it all over again with the line that has 3 stars and does 4 lines, then starts over with 5 stars, etc. I *think* what you meant was only the loop with the lines and the one for spaces and stars; I don't think you need the outermost loop.

Now, if your problem is that the stars on the different lines don't line up, then likely you are outputting this (or looking at the output) in some place that does "proportional spacing"; this means that each character only takes up as much space as its font thinks it needs, and stars and spaces won't take up the same amount of space. If you put the output into a file, and then into, say, Windows Notepad, they ought to line up because Notepad's default font is a "fixed-width" font, i.e., has the same horizontal space taken up by each character.

rc
James.Seung Lee
Greenhorn

Joined: Sep 30, 2011
Posts: 2
For the first one (3, 4) it should come out like this: (I don't think my algorithm for spacing in the method is right)
Note: this should be center aligned haha, it comes out left aligned.

*
***
*****
*******
***
*****
*******
*********
*****
*******
*********
***********
*
*
*******
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8407
    
  23

For the first one (3, 4) it should come out like this:

I'm probably just being thick here, but I still don't understand exactly what 'segment' and 'height' are.
Am I right in thinking that the 'segments' are the triangles that get progressively larger, and 'height' is the number of lines used to draw a segment?

If so, you might want to break down the process for drawing a tree into its component parts (something you should get into the practise of doing for ALL programs). Maybe something like, in my bad pseudo-code:I'll leave the rest to you, but it may help you see things a bit more clearly, rather than trying to handle all those nested loops.

Winston
Ralph Cook
Ranch Hand

Joined: May 29, 2005
Posts: 479
Please look at the second paragraph of my response above. It addresses the spacing problem.

I don't see a way to upload an image from my machine to javaranch or I'd show you, but when I run this the output looks center-aligned with a fixed-width font.

Both your responders so far have asked what a segment is; knowing that is essential to understanding what you are trying to do, though it may not be related to the spacing problem.

rc
john paschal
Greenhorn

Joined: Sep 12, 2012
Posts: 1
here is the code using Scanner to obtain the number of segments and height from the user. copy and paste messed up my indenting but you get the idea





Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40034
    
  28
Welcome to the Ranch , john paschal. You may be a little late with your reply. I shall try to enhance it with the code button; if I succeed you can see how much better it looks.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Problem with spacing