Meaningless Drivel is fun!*
The moose likes Beginning Java and the fly likes for statements Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "for statements" Watch "for statements" New topic
Author

for statements

charlie mills
Ranch Hand

Joined: Nov 01, 2008
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
internet detective
Marshal

Joined: May 26, 2003
Posts: 30356
    
150

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 .


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11250
    
  16

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.


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

Joined: Nov 01, 2008
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

Joined: Nov 01, 2008
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

Joined: Oct 13, 2005
Posts: 38449
    
  23
Have you tried it? What happened?

Hint: changeto
charlie mills
Ranch Hand

Joined: Nov 01, 2008
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

Joined: Nov 01, 2008
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

Joined: Oct 13, 2005
Posts: 38449
    
  23
Well done, getting it to work and please show us the working code.
charlie mills
Ranch Hand

Joined: Nov 01, 2008
Posts: 49
sure,

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

System.out.print("*");
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: for statements