Results 1 to 8 of 8
  1. #1
    Hello is offline Member
    Join Date
    Oct 2011
    Posts
    4
    Rep Power
    0

    Default 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:
    Nested for loop problem.-forprob.jpg

    Here is my code so far,
    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();
    	}
    
    
    
    }
    
    }
    Example output:

    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.

  2. #2
    tnrh1 is offline Senior Member
    Join Date
    Aug 2011
    Posts
    251
    Rep Power
    4

    Default 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.

  3. #3
    Iron Lion is offline Senior Member
    Join Date
    Nov 2010
    Posts
    210
    Rep Power
    5

    Default Re: Nested for loop problem.

    Quote Originally Posted by tnrh1 View Post
    btw: change those long names! I'm using "k" and "i" for my indexes.
    Nothing wrong with descriptive names for loop counters if it makes the purpose of the loop more clear.

  4. #4
    tnrh1 is offline Senior Member
    Join Date
    Aug 2011
    Posts
    251
    Rep Power
    4

    Default Re: Nested for loop problem.

    Quote Originally Posted by Iron Lion View Post
    Nothing wrong with descriptive names for loop counters if it makes the purpose of the loop more clear.
    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);
    Java Code:
    for(int i=0;i<=k;k++)
        System.out.println(i);
    Work as you like.

  5. #5
    acmohan is offline Member
    Join Date
    Jul 2011
    Posts
    26
    Rep Power
    0

    Default Re: Nested for loop problem.

    int k=1;
    for(i=1;...;..){
    for(j=1;..;..){
    if(k<=x)
    System.out.printf("%"+i+"d",k);
    k++;
    }
    System.out.println();
    }
    this will work as u required.this s jst sample. try in this way.jst try implementing

  6. #6
    Hello is offline Member
    Join Date
    Oct 2011
    Posts
    4
    Rep Power
    0

    Default Re: Nested for loop problem.

    Thanks for helping me out here.
    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.
    Fair enough, it works well, but the only problem now is this:

    Nested for loop problem.-prob.jpg

    As you can see on the output pic, i get blank-lines on output. What could be the problem?

    Kind Regards Danny.

  7. #7
    tnrh1 is offline Senior Member
    Join Date
    Aug 2011
    Posts
    251
    Rep Power
    4

    Default 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++)

  8. #8
    tnrh1 is offline Senior Member
    Join Date
    Aug 2011
    Posts
    251
    Rep Power
    4

    Default 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:

    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();
    		}
    			
    	}
    This loop will run as many times as it needs.
    Last edited by tnrh1; 11-03-2011 at 06:10 PM.

Similar Threads

  1. Nested loop
    By Shasool in forum New To Java
    Replies: 2
    Last Post: 10-23-2011, 06:10 PM
  2. Nested loop problem
    By jim01 in forum New To Java
    Replies: 0
    Last Post: 04-17-2011, 04:38 AM
  3. Nested Loop
    By sehudson in forum New To Java
    Replies: 2
    Last Post: 03-11-2011, 04:39 AM
  4. can some one help me with nested loop?
    By keycoffee in forum New To Java
    Replies: 10
    Last Post: 01-25-2010, 03:49 AM
  5. Nested For Loop
    By yuchuang in forum New To Java
    Replies: 1
    Last Post: 07-08-2007, 02:11 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •