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 For Loop Problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "For Loop Problem" Watch "For Loop Problem" New topic
Author

For Loop Problem

Dennis Myhand
Greenhorn

Joined: Sep 18, 2005
Posts: 7
I need to do something specific with a for loop. This is for an assignment in a class I am in, I want to say that upfront so there is no confusion. I need to make a forloop which prints the following:

*
**
***
****
*****
******
*******
********
*********
**********

It then is rotated 90 degrees through the next instance of the loop until it has gone a full 360.

What I have now does the sequence above. I know I need a nested for loop, but I am not sure how to implement this. The code I currently have is below. Any help is greatly appreciated. Thanks, Dennis

Code follows:

public class Forloop
{
public static void main( String args[] )
{

for ( int line_no = 1; line_no <= 10; line_no++ )
{
String print = "";
for ( int stars = 1; stars <= line_no ; stars++ )

{

print += "*";
}

System.out.println(print);
}

}

}
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
It looks like the above program will print the triangle made of *'s as you have shown. How can you change this to print the next "rotated triangle"? If I were doing this, I would just make another set of nested for loops similar to what you already have.

Does this make any sense? Think about it for a little bit and see what you can come up with. Feel free to use this forum as a sounding board for your ideas.

Layne


Java API Documentation
The Java Tutorial
Dennis Myhand
Greenhorn

Joined: Sep 18, 2005
Posts: 7
Yes, I understand I need another set of forloops nested in the forloop I have, and I stated that in my post. I have tried turning the loops every way I can think of and I get nothing but errors about line_no, print, and stars already being defined in main. Take those out of the loop I added and I get even more errors. I am not seeing HOW to implement a nested loop. I tried googling for this information and I only seem to be redirected to a Pharmacy site in India. Do you know of a specific site where this information might be?
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336


Yes, I understand I need another set of forloops nested in the forloop I have, and I stated that in my post

Why do you need further nested loops? Assuming you have already successfully created one of the four triangles, you already have the code you need? Can you see how that could be done without nesting more loops?


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Dennis Myhand
Greenhorn

Joined: Sep 18, 2005
Posts: 7
No.
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3742
    
  16
Try drawing what you want the output to be and then think how you could modify your for loop to produce that output.
Remember that for loops can decrement as well as increment.


Joanne
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

Originally posted by Dennis Myhand:
No.


Well, try looking again, bearing in mind what Joanne Neal has suggested. You might find it clearer if you write out in pseudo-code what you are trying to do before trying to write it in Java.
Dennis Myhand
Greenhorn

Joined: Sep 18, 2005
Posts: 7
I'm sorry folks, but your hints are too subtle for me. Any changes I make to the code seem to cause it to do really wierd things or stop working altogether. Can someone please point me to a tutorial?
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

You are not going to get a tutorial for something as specific as you need. There are plently of tutorials which will include the basics of loops (Sun's Java Basics tutorial, for example), but I'm assuming from what you've already posted you now this anyway. Perhaps you could post what you are trying (and is giving you so much trouble) then some kind people here might point you in the right direction.
[ September 19, 2005: Message edited by: Paul Sturrock ]
Dennis Myhand
Greenhorn

Joined: Sep 18, 2005
Posts: 7
What I need to do:

Display the following:


What I have done, code wise:


As you can see from the commented out section, I have tried adding another loop, I have tried ++line_no, I have tried ++stars, I have tried line_no--, I have tried stars--, I have tried --line_no and --stars. I am at a loss as to what to try next.

//JAM -- Added [CODE] and [/CODE] tags.
[ September 19, 2005: Message edited by: Joel McNary ]
Joel McNary
Bartender

Joined: Aug 20, 2001
Posts: 1824

Well, to flip vertically should be easiest. Right now, your outer loop goes from 1 to 10. What happens if your outer loop goes backwards, from 10 to 1? Give it a try and find out.

Then, you have the slightly harder problem of right-aligning the things. Here, remember, that you have to print spaces before printing the asterisks. How many spaces? Well in the first line, there's 9 spaces and 1 asterisk., in the second line there's 8 spaces and 2 asterisks. Looks like the relationship here is (10 - lineNo) spaces and (lineNo) asterisks. Sounds like a job for two separate loops nested inside the outer loop.

Finally, you have to vertically flip the right-aligned output. That should be simple, now...we've already solved that problem before.

Hope that this helps.


Piscis Babelis est parvus, flavus, et hiridicus, et est probabiliter insolitissima raritas in toto mundo.
Dennis Myhand
Greenhorn

Joined: Sep 18, 2005
Posts: 7
"Well, to flip vertically should be easiest. Right now, your outer loop goes from 1 to 10. What happens if your outer loop goes backwards, from 10 to 1? Give it a try and find out."

I gave it a try, as I stated in the long post. It does not work. I really don't know another way to say this. It does not work.


"Sounds like a job for two separate loops nested inside the outer loop"

This is what I have been asking how to do since my first post. I am not understanding how to do this, and I really don't know any other way to say that. I am sorry if I seem irritated, but I find very little help in every answer to my posts saying things like "This should be easy." Perhaps it should be easy. I do not find it so. If what I have asked is beyond the scope of this forum, PLEASE LET ME KNOW.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 19073
    
  40

As you can see from the commented out section, I have tried adding another loop, I have tried ++line_no, I have tried ++stars, I have tried line_no--, I have tried stars--, I have tried --line_no and --stars. I am at a loss as to what to try next.


I think the problem is that you probably are not sure how the original code works... did you happen to write it? It is not as simple as changing a variable, or changing a postfix to prefix, etc.

I suggest you figure out how the original code works, and then change the program to do the other triangles. Once you have 4 separate programs that does the 4 different triangles, and understand it, then put it together as one program.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Dennis Myhand
Greenhorn

Joined: Sep 18, 2005
Posts: 7
Okay, since I have yet to get anything even remotely close to a striaght answer, how do I contact a moderator to close or delete this topic?
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9053
    
  12
How to nest loops


Your code above (as you probably already know) prints

*
**
***
****
*****
******
*******
********
*********
**********

It looks like you already have the basics of a nested for loop.

Now, after you finish your first outer for loop, you need to run a new outer loop from 0 to 10 while running the inner loop from (10 - line_no) to 0, decrementing stars by one each time.

Does that help?

By the way, I think it would be easier for you if you just print the star each time and do a println at the end of the loop rather than concatenating the stars and printing the whole line.
[ September 19, 2005: Message edited by: Marilyn de Queiroz ]

JavaBeginnersFaq
"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
Originally posted by Dennis Myhand:
[QBI gave it a try, as I stated in the long post. It does not work. I really don't know another way to say this. It does not work.

[/QB]


As others have pointed out, you do NOT need extra nested loops. Perhaps it will help if you write four separate programs to do each triangle. One you figure out how each one works, then you can combine it into a single program. I think Joel's hints should help you getting one of them at least.

Layne
[ September 19, 2005: Message edited by: Layne Lund ]
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
Originally posted by Dennis Myhand:
Okay, since I have yet to get anything even remotely close to a striaght answer, how do I contact a moderator to close or delete this topic?


You can delete the post yourself if you want. But what is the point? As you can see we are more than willing to help, but I think we are helping as much as we can without just giving you the answer. If you are really interested in learning how to program in Java, you should appreciate that self-discovery is part of the learning process. I hope my comments above will help clarify some of the things you are having problems with. The main point is that you only need two nested loops for each triangle. However, each pair does not need to be nested in any of the others as you have already tried.

Keep Coding!

Layne
Hentay Duke
Ranch Hand

Joined: Oct 27, 2004
Posts: 198
Okay, since I have yet to get anything even remotely close to a striaght answer, how do I contact a moderator to close or delete this topic?


By straight answer you mean the exact code of my homework assignment then you are correct. However you have been given enough hints to make some progress and come back with something more specific than "it doesn't work". In particular the suggestion to try it in four seperate programs was a good one, and the one to start a loop at 10 and decrement is very key. Do you know how to do this kind of loop? Something like

will come in handy!

Don't give up. I remember doing a similar problem when I was learning, I'm sure most of us have. The joy comes in figuring it out and seeing the solution that seemed so hard at one point.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: For Loop Problem