# for statements

charlie mills
Ranch Hand
Posts: 49
I want to display spaces on the screen, with the first line having spacesonfirstrow which has been calculated, then on the next row would be one fewer spaces, and the next row one fewer etc. (decrease by 1 each time). it would keep going until it got down to one space and then it would stop. i thought the code below would be correct, but its not and i cant see where i'm going wrong

for (int spaces = spacesOnFirstRow - 1; spaces >=1; spaces--)
{
System.out.print(" ");
}

Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34665
366
Charlie,
That prints a bunch of spaces all one line. You would want two loops (one controls lines and one controls # characters per line.

You might want to test this out with * instead of space since spaces are invisible .

fred rosenberger
lowercase baba
Bartender
Posts: 12143
30
Jeanne is right. One thing you'll learn how to do is break a problem down into smaller problems, and tackle them one at a time.

so for this, there are two problems... 1) How many spaces should i print on a row, and 2) print 'x' spaces.

I think from your other thread, you know how to do #2 - you have a loop that prints a single space 'x' times.

Now you want to solve part 1... write a loop that prints something like

print 5 spaces here
print 4 spaces here
print 3 spaces here
print 2 spaces here
print 1 spaces here

If you have a loop that can do that (and the printing of the words can be done with one statement), then you can basically drop your solution to the other problem into this one.

that should give you some tings to think about.

charlie mills
Ranch Hand
Posts: 49
I do have another loop which controls the lines. that part works fine, i just cant get the number of spaces to drop by one on each line.

charlie mills
Ranch Hand
Posts: 49
I've put the loop for the number of spaces inside the loop that controls the lines. i know the first loop works but i still cant work out how to do the inner loop. is what i've done even remotely correct?

for (int rows = 1; rows < numberOfRows; rows++)
{

for (int spaces = spacesOnFirstRow; spaces <= spacesOnFirstRow - rows; spaces--)
{
System.out.print(" ");
}

System.out.print("*");

Campbell Ritchie
Sheriff
Posts: 49367
62
Have you tried it? What happened?

Hint: changeto

charlie mills
Ranch Hand
Posts: 49
yeah, i tried it and it doesnt print any spaces at all, for any rows.
i can't change it to println because I want to print other stuff after the for loop.

charlie mills
Ranch Hand
Posts: 49
i've done it, i was trying to make it way more complicated than it need to be. thanks for all your help

Campbell Ritchie
Sheriff
Posts: 49367
62
Well done, getting it to work and please show us the working code.

charlie mills
Ranch Hand
Posts: 49
sure,

for (int rows = 1; rows < numberOfRows; rows++)
{for (int spaces = 0; spaces < spacesOnFirstRow - rows; spaces++)
{
System.out.print(" ");
}

System.out.print("*");