File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Small looping question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Small looping question" Watch "Small looping question" New topic
Author

Small looping question

Tod Novak
Greenhorn

Joined: Dec 14, 2006
Posts: 13

I'm trying...honestly I am. But maybe some people just aren't cut out to learn programming. I'm really starting to wonder.

Anywho... I'm trying to write a small program that will print out a wedge of stars. Seems easy enough.

This is what I have so far:



Output should be:
Initial number of stars:
7
*******
******
*****
****
***
**
*


All I can figure out how to do is print the first line.

I know I'm missing something easy, I just can't wrap my head around it.


"-Of all the things I've lost...I miss my mind the most!"
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18712
    
  40

All I can figure out how to do is print the first line.


Well, your loop only does one line. Can you use a loop to go through all the lines? Basically, have a loop within a loop?

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

You have code that will do something once, and you want it to do that thing multiple times. That's exactly what loops are for.

So if you put your code that prints a line inside a loop, then you can print multiple lines.


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Garrett Rowe
Ranch Hand

Joined: Jan 17, 2006
Posts: 1296
Another way to look at it is to encapsulate what you already know how to do in a method so that you can call it with different parameters. A call to the method might look like:

print(7, "*")

and the method signature would look like:

public void print(int times, String element)

once you've got that working (hint hint, you have the code for that one pretty much written) you can work out a loop that calls the method with different arguments:


until you're done. To my little brain that's easier to understand than nested loops, as always though, your mileage may vary.


Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them. - Laurence J. Peter
Tod Novak
Greenhorn

Joined: Dec 14, 2006
Posts: 13

Thanks all for the help so far.

The lessons I'm following haven't touched on the 'for' loops yet, so I'm kinda stuck with 'if' and 'while' I think.

It's the nesting that's causing me such a headache. I'll keep punching it out though.
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by Tod Novak:
...I'm kinda stuck with 'if' and 'while' I think.

It's the nesting that's causing me such a headache...

You can do this using nested while loops. The nesting itself is straight forward...

But you need to put some thought into what the while conditions are, and the other code that goes inside the loop bodies.
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11229
    
  16

'for' and 'while' loops are really interchangeable. Generally, if you know the exact number of loops, you'd use a 'for', otherwise a 'while', but that's really not written in stone.

here's how they would translate:


vs


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Tod Novak
Greenhorn

Joined: Dec 14, 2006
Posts: 13

Woot!

Finally got it!



Not sure if it's as pretty as it could be, but it works.

Thank you all again for the guidance.
 
wood burning stoves
 
subject: Small looping question