Win a copy of The Way of the Web Tester: A Beginner's Guide to Automating Tests this week in the Testing forum!

# Printing a Diamond - Trouble with First Nested Loop

Jenny Daniels
Greenhorn
Posts: 5
Hey hey friendly javaranch,

I'm trying to teach myself java this fall/winter and am completely stuck with this nested loop problem. While this code below excutes the rectangle of astericks in the middle that I want, I have no clue where to begin in terms of crafting the rest of this shape. I reread the chapters in my text on decisions and iteration, but I still am dead stuck. Any suggestions would be greatly appreaciated. Pic related; it's what I want to print in the end.

Jeff Verdegan
Bartender
Posts: 6109
6
• 1
Your first step is to write down in English, in very precise, simple steps, exactly how to do this "manually", without any regard to Java or any other programming language.

You might start with literal steps like, "Draw one star on the first line. Draw two stars on the second line," and so on. Then you might refine those steps as you discover the relationships between what goes where and which line you're on, for example, "Draw N stars on the Nth line." Once you've got that worked out, and you can follow the steps exactly as written with pencil and paper and get the result you want, then you're ready for the easy part--translating it to Java.

I would suggest two additional things:

1) Start with something simpler, like just a diamond of stars, without the border or box in the middle. If that proves troublesome, go simpler still, like a triangle of starts--1 on line 1, 2 on line 2, etc.--and then reverse that triangle so that the first line has a bunch of spaces and then the single star on the left, and so on.

2) For each pattern that you do, start with a small number of rows, like 4 or 5. That will make it easier to work through by hand.

I know that may sound like a lot of extra work, but I promise you, it will pay off. If you just jump in head first and try to do it all at once by sitting down and typing public static void main(String[] args), you'll just be setting yourself up for frustration and spinning your wheel.s

Kemal Sokolovic
Bartender
Posts: 825
5
Crossposting

Jeff Verdegan
Bartender
Posts: 6109
6
• 1
@Jenny Daniels: As a courtesy to the folks here who volunteer their time to answer your questions, please BeForthrightWhenCrossPostingToOtherSites(←click). Thanks!

Kemal Sokolovic
Bartender
Posts: 825
5
OP has just earned a title of time waster (at my side). Even the old thread started by OP has been crossposted to other site, and that's not the way to say thanks to those who are trying to help.

Jenny Daniels
Greenhorn
Posts: 5
Jeff Verdegan wrote:Your first step is to write down in English, in very precise, simple steps, exactly how to do this "manually", without any regard to Java or any other programming language.

You might start with literal steps like, "Draw one star on the first line. Draw two stars on the second line," and so on. Then you might refine those steps as you discover the relationships between what goes where and which line you're on, for example, "Draw N stars on the Nth line." Once you've got that worked out, and you can follow the steps exactly as written with pencil and paper and get the result you want, then you're ready for the easy part--translating it to Java.

I would suggest two additional things:

1) Start with something simpler, like just a diamond of stars, without the border or box in the middle. If that proves troublesome, go simpler still, like a triangle of starts--1 on line 1, 2 on line 2, etc.--and then reverse that triangle so that the first line has a bunch of spaces and then the single star on the left, and so on.

2) For each pattern that you do, start with a small number of rows, like 4 or 5. That will make it easier to work through by hand.

I know that may sound like a lot of extra work, but I promise you, it will pay off. If you just jump in head first and try to do it all at once by sitting down and typing public static void main(String[] args), you'll just be setting yourself up for frustration and spinning your wheel.s

Thank you for this. I am able to create a diamond of pure stars that's j wide, represented here:

I am still trying to work out printing two stars on the first line, then increasing by 4 stars with each line like the included image.

Kemal Sokolovic wrote: OP has just earned a title of time waster (at my side). Even the old thread started by OP has been crossposted to other site, and that's not the way to say thanks to those who are trying to help.

This is true! Sorry, I am used to receiving no help at all so I thought one repost was not too bad. However I do understand how that may frustrate those who could help so I really do apologize. I will delete the other thread as soon as I return to a wifi connection where I am not banned (I guess you reported me?) I will be forthright in the future; I am sorry.

Jeff Verdegan
Bartender
Posts: 6109
6
Jenny Daniels wrote:
I am still trying to work out printing two stars on the first line, then increasing by 4 stars with each line like the included image.

So, 2 stars, then 6, then 10, and so on?

Time for some basic algebra. What's the equation for a line with the following points?

Jenny Daniels
Greenhorn
Posts: 5
Jeff Verdegan wrote:

So, 2 stars, then 6, then 10, and so on?

Time for some basic algebra. What's the equation for a line with the following points?

y = 4x +2 Good look! Okay, so I believe I have know how to tackle the stars.

Jeff Verdegan
Bartender
Posts: 6109
6
Jenny Daniels wrote:y = 4x +2 Good look! Okay, so I believe I have know how to tackle the stars.

Excellent!

(That's one thing I think we need to start including in our standard "work it out by hand first, without any reference to Java" responses: Even though we're not writing the steps in Java, we can still use algebra, trigonometry, logic, etc., and all their attendant operations. I think people tend to take the "write the steps in English first" to an extreme, and write it in a more conversational form, which ends up being too fuzzy to convert easily to Java.)

Jenny Daniels
Greenhorn
Posts: 5
Half of the diamond done but I'm having some trouble visualizing the code for the second half. I know it should be an easy modification of the variables but my mind is working a bit slowly today...

Thanks for all the help for far, y'all.

>