Results 1 to 8 of 8
Thread: Nested for loop problem.
- 10-26-2011, 11:29 PM #1
Member
- Join Date
- Oct 2011
- Posts
- 4
- Rep Power
- 0
Nested for loop problem.
Hello there, I've been working on this for a while now, and I am still wondering how to solve the problem.
I want to create this:

Here is my code so far,Example output:Java Code:import java.util.Scanner; public class Testing { public static void main (String args[]){ Scanner input = new Scanner(System.in); int max; System.out.print("Max value: "); max = input.nextInt(); printNumbersIncreasing(max); } public static void printNumbersIncreasing(int x){ for (int lineNumber = 1; lineNumber <= x; lineNumber++){ for (int currentField = 1; currentField <= lineNumber; currentField++){ System.out.printf ("%" + lineNumber + "d", currentField); } System.out.println(); } } }
____Java Code:Max value: 5 1 1 2 1 2 3 1 2 3 4 1 2 3 4 5
As you can see, my counter starts over each line, how do i get the counter to continue on each line?
Kind Regards Danny.
- 10-27-2011, 12:06 AM #2
Senior Member
- Join Date
- Aug 2011
- Posts
- 248
- Rep Power
- 2
Re: Nested for loop problem.
You can print your loops indexes since they reset every loop.
You need another variable that will be raised in 1 after you print him in the nested loop.
for ..
for ..
{
print(counter)
counter++
}
btw: change those long names! I'm using "k" and "i" for my indexes.
- 10-27-2011, 12:08 AM #3
Senior Member
- Join Date
- Nov 2010
- Posts
- 210
- Rep Power
- 3
- 10-27-2011, 12:32 AM #4
Senior Member
- Join Date
- Aug 2011
- Posts
- 248
- Rep Power
- 2
Re: Nested for loop problem.
I didn't said it's wrong.
Using short names helps to read the code faster, save time in writing it and the chance to type it wrong is much lower.
But it's not a demand it's an advice.
Java Code:for(int LongVariableName=1;LongVariableName<=MaximumNumber;LongVariableName++) Sytem.out.println(LongVariableName);Work as you like.Java Code:for(int i=0;i<=k;k++) System.out.println(i);
- 10-27-2011, 11:48 AM #5
Member
- Join Date
- Jul 2011
- Posts
- 26
- Rep Power
- 0
Re: Nested for loop problem.
this will work as u required.this s jst sample. try in this way.jst try implementingint k=1;
for(i=1;...;..){
for(j=1;..;..){
if(k<=x)
System.out.printf("%"+i+"d",k);
k++;
}
System.out.println();
}
- 10-27-2011, 02:47 PM #6
Member
- Join Date
- Oct 2011
- Posts
- 4
- Rep Power
- 0
Re: Nested for loop problem.
Thanks for helping me out here.
Fair enough, it works well, but the only problem now is this:Java Code:import java.util.Scanner; public class Boka { public static void main (String args[]){ Scanner input = new Scanner(System.in); int max; System.out.print("max value: "); max = input.nextInt(); printNumbersIncreasing(max); } //Main method end public static void printNumbersIncreasing(int x){ int counter = 1; for (int i = 1; i <= x; i++){ for (int j = 1; j <= i; j++){ if (counter <= x){ System.out.printf ("%" + i + "d", counter); counter++; } //if statment end } // Inner for loop end System.out.println(); } //outer for loop end } //Method end } //Class end.

As you can see on the output pic, i get blank-lines on output. What could be the problem?
Kind Regards Danny.
- 10-27-2011, 07:09 PM #7
Senior Member
- Join Date
- Aug 2011
- Posts
- 248
- Rep Power
- 2
Re: Nested for loop problem.
That's cause of the first for loop, he has to stop much before he equals to x.
You can fix it by adding a boolean variable, let say flag and equal it to true.
Then add an "else" to your if statement (counter<=x), and inside the else type flag = false;
Now before the system.out.println(); add an if statement that ask if the the flag is true.(if flag).
Why we did all that?when if (counter <= x) give you false you still printing the new lines and your first for loop keep running.
I'm sure that there is a better solution, I will think about it and edit my post.
The answer is definitely in this line: for (int i = 1; i <= x; i++)
- 11-03-2011, 05:06 PM #8
Senior Member
- Join Date
- Aug 2011
- Posts
- 248
- Rep Power
- 2
Re: Nested for loop problem.
After few days I finally got an answer, thanks to 2 guys josAH and pbrockway2.
You asked if counter is <= to x because your loop runned much more times then it should be.
This one is the correct answer:
This loop will run as many times as it needs.Java Code:public static void print (int num) { int counter = 1; for(int i=1;i<=Math.round(Math.sqrt(num*2));i++) { for(int k=1;k<=i;k++) { System.out.print(counter+" "); counter++; } System.out.println(); } }Last edited by tnrh1; 11-03-2011 at 05:10 PM.
Similar Threads
-
Nested loop
By Shasool in forum New To JavaReplies: 2Last Post: 10-23-2011, 05:10 PM -
Nested loop problem
By jim01 in forum New To JavaReplies: 0Last Post: 04-17-2011, 03:38 AM -
Nested Loop
By sehudson in forum New To JavaReplies: 2Last Post: 03-11-2011, 03:39 AM -
can some one help me with nested loop?
By keycoffee in forum New To JavaReplies: 10Last Post: 01-25-2010, 02:49 AM -
Nested For Loop
By yuchuang in forum New To JavaReplies: 1Last Post: 07-08-2007, 01:11 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks