aspose file tools*
The moose likes Beginning Java and the fly likes Nested Loops Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Nested Loops" Watch "Nested Loops" New topic
Author

Nested Loops

Elise Wilson
Greenhorn

Joined: Oct 12, 2005
Posts: 5
I can not figure out what is the prooooblem... Please help...

I need this pattern

1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
1 2 3 4 5 6

and here is my code...

import javax.swing.JOptionPane;

public class forLoop {
public static void main(String[] args) {
int row;
int column;

String outputPattern = "Pattern I\n";

for (row = 1; row <= 6; row++) {
for (column = 1; column <= row; column++) {
outputPattern += column + " ";
}
outputPattern += "\n";

System.out.println(outputPattern);
}
}
}

Instead of looking like the above pattern, it looks like:

Pattern I

1

Pattern I

1
1 2

Pattern 1

1
1 2
1 2 3

etc... etc... etc...

Please show me what a little mistake I am making...

Thanks in advance
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 19064
    
  40

Well, I could give you a hint, but you are already so close...

Basically, your print statement is in the wrong place. It is being called many times. Move your print statement outside of the outer loop.

Henry


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

Joined: Oct 12, 2005
Posts: 5
OH MY GOODNESS!!! I knew it was something that was sooooo close...

THANK YOU SO MUCH!!! WOW @ your kindness... Other forums have some very *not so nice* people...



marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by Elise Wilson:
... Other forums have some very *not so nice* people...

Yeah, I think we can guess where those "other forums" might be.


"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
Elise Wilson
Greenhorn

Joined: Oct 12, 2005
Posts: 5
Stumped again...

for (row = 1; row <= 6; row++) {
for (column = 6; column >= row; column--) {
outputPattern += column + " ";
}
outputPattern += "\n";
}

Looks like:
6 5 4 3 2 1
6 5 4 3 2
6 5 4 3
6 5 4
6 5
6

"SHOULD" look like this:

1
2 1
3 2 1
4 3 2 1
5 4 3 2 1
6 5 4 3 2 1

Since it is the flip of the first one, i did just that... It's not working... Please explain... All this +, >, -- is confusing...

Elise Wilson
Greenhorn

Joined: Oct 12, 2005
Posts: 5
Should look like (what happened to what I put):

For S&G purposes there aren't suppose to be * but it wouldn't take the spaces...

**********1
********2*1
******3*2*1
****4*3*2*1
**5*4*3*2*1
6*5*4*3*2*1
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

You can keep your space formatting intact by using the CODE tags when posting (or editing). So this is what you want...

Right?

Take this one row at a time. Suppose row = 1, and column goes from 6 to 1. For column values 6 through 2, we want a space. But for column = 1, we want the number 1. The trick is expressing this (the pattern of 5 spaces followed by the number "1") in terms of row and column values.

Next, suppose row = 2, and column again goes from 6 to 1. For column values 6 through 3, we want a space. But for column values 2 and 1, we want the numbers 2 and 1 respectively.

Do you see a pattern?
[ October 13, 2005: Message edited by: marc weber ]
Elise Wilson
Greenhorn

Joined: Oct 12, 2005
Posts: 5
By George I think I got it... My teacher doesn't "break it down" he just says to do it...

Thanks for all you guys help...

marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by Elise Wilson:
... My teacher doesn't "break it down" he just says to do it...

Learning to approach problems in this manner is one of the more valuable skills you can develop as a programmer.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Nested Loops