# Homework assignment (beginner)

• May 30th, 2011, 02:30 AM
z3ohyr84
Homework assignment (beginner)
I'm taking a course and I'm in about my 3rd week, totally new to this otherwise.

The assignment was as follows: Including a do-while statement, create a program that asks for user input for an integer, then prints a diamond to the console based on that integer. For example, if the integer is 5, it should be 5 lines into the middle.

Code :

``` * * * * * * * * * * * * * * * *```

Without further delay, here is what I've come up with for the first half of the diamond only. I'm aware it's so far from optimal that it's probably hilarious, but I've been struggling with this for a long time now and want to finish this if it can work.

The comments should explain my methodology--and lastly, I greatly appreciate any guidance.

Oh, I should say my result looks like this... the spaces aren't happening at the beginning.

Code :

```* * * * * * * * *```

Code Java:

```  import javax.swing.JOptionPane;   public class doWhile {   public static void main(String[] args) { String symbol = "*", spacer = " ", gap = " ";   String uinput = JOptionPane.showInputDialog("Enter an integer."); int size = Integer.parseInt(uinput); int spaceCount = size; int gapCount = 1;   // This first 'for' statement places the first asterisk. I do this separately because // it makes it easier later on when there is a 'gap' in between each asterisk, which // has to go up by 2 in the loops.   for (int a = 1; spaceCount == 0; spaceCount--); { System.out.print(spacer); // Prints a space until the count is 0. } System.out.print(symbol); // Prints the first asterisk in the line.   spaceCount = size; // Resets the spaceCount to be the size again.   for (int b = 1; spaceCount == 0; spaceCount--); { System.out.println(spacer); // Prints a space until the count is 0. } size--; // Size gets reduced as the 1st diamond line is complete.   spaceCount = size; // spaceCount becomes new size.     do {   // Same methodology occurs for the first half of the diamond. // gapCount is introduced to go up by 2 with each line, as this // is what happens internally in a diamond shape.   gapCount = gapCount + 2;   for (int c = 1; spaceCount == 0; spaceCount--); // Spaces decrease as gap size increases { System.out.print(spacer); // Prints spacer, repeats until spaceCount is 0. }   spaceCount = size; // Resets spaceCount to the size.   System.out.print(symbol); // Prints 1st asterisk in line.   for (int d = 1; gapCount > 1; gapCount--) { gap = gap + (" "); // Gap becomes single space, plus 2 additional spaces.   }   System.out.print(gap); // Prints the gap between asterisks   System.out.print(symbol); // Prints 2nd asterisk in line   for (int e = 1; spaceCount == 0; spaceCount--); // Spaces decrease as gap size increases { System.out.println(spacer); // Prints spacer, repeats until spaceCount is 0. }   size--; // Size is reduced by one more line.   spaceCount = size; // Spacecount becomes new size.     } while (size > 0); // Whole process runs until size no longer has a value.   System.exit(0); }     }```
• May 30th, 2011, 07:06 AM
Norm
Re: Homework assignment (beginner)
Code :

` for (int a = 1; spaceCount == 0; spaceCount--);`
No ending ; here. It ends the WHOLE if statement
What is the variable a used for?

How does the condition part of the if statement control the looping? (spaceCount == 0)

Also look at your use of print vs println.

Other ideas to make debugging easier. Instead of print blanks, print a character:
spacer = "S", gap = "G";

gap = gap + ("gg");
Then you can see better what is happening.
• May 30th, 2011, 09:36 AM
z3ohyr84
Re: Homework assignment (beginner)
Thanks for the help!

The print vs println statements were done intentionally because I only want it to go down to the next line after finishing the final spacer in each line. That spacer might be unnecessary since you don't need to have a space after the 2 asterisks have been printed, but for whatever reason I included it. So then it goes down a line, followed by several print commands, followed by a println again to go down. Did I do this incorrectly?

I will take your advice for entering a value to debug with and see if that sparks something.
• May 30th, 2011, 12:50 PM
z3ohyr84
Re: Homework assignment (beginner)
I've been trying to debug it all morning with your suggestions and while they're helping, I'm still stuck. So I tried to narrow it down using just my initial line to print x number of spaces, an asterisk, and x number of spaces again.

Can anyone take a look at this and let me know why this outputs a single asterisk with no spaces (spaces are represented by 's', gaps are 'g' but don't come into play yet).

for (int a = 1; spaceCount < 1; spaceCount--) <-- my problem may be here, but I'm not sure why. The initialization is just to get it going, so I thought a = 1 would be fine. I checked and spaceCount does = the user's input, so if I test with the number 5 it does equal 5 immediately before this 'for' loop runs. So in theory spaceCount should be decrementing until it becomes 0, and each time it should be printing an 's'--why is this not so?

Code :

``` public static void main(String[] args) { String symbol = "*", spacer = "s", gap = "g";   String uinput = JOptionPane.showInputDialog("Enter an integer."); int size = Integer.parseInt(uinput); int spaceCount = size; int gapCount = 1;   JOptionPane.showMessageDialog(null, spaceCount);   // This first 'for' statement places the first asterisk. I do this separately because // it makes it easier later on when there is a 'gap' in between each asterisk, which // has to go up by 2 in the loops.   for (int a = 1; spaceCount < 1; spaceCount--) { System.out.print(spacer); // Prints a space until the count is 0. }   System.out.print(symbol); // Prints the first asterisk in the line.   spaceCount = size; // Resets the spaceCount to be the size again.   for (int a = 1; spaceCount < 1; spaceCount--) { System.out.print(spacer); // Prints a space until the count is 0. }```
• May 30th, 2011, 01:07 PM
Norm
Re: Homework assignment (beginner)
Code :

`for (int a = 1; spaceCount < 1; spaceCount--)`

Look at the condition for this if statement. If true the loop goes around again.
What is the value of spaceCount when you enter the for () statement
I think you want to go around the loop while spaceCount is > 0
• May 30th, 2011, 01:32 PM
z3ohyr84
Re: Homework assignment (beginner)
I see what you're saying--when I read an explanation about the termination point of a for statement, I interpreted it so that the loop will continue until the middle boolean occurs, which is why I had it set to < 1, so that it would continue subtracting from spaceCount until spaceCount was 0. I see how it works now, and fortunately the first half of my diamond now completes with those adjustments. Should be able to do the same with some other changes and get the bottom half going.

Thanks so much for this!