TwoDarray or loop


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 3 of 3

Thread: TwoDarray or loop

Hybrid View

  1. #1
    Join Date
    Feb 2006
    Posts
    9

    Question TwoDarray or loop

    Hi ,
    any one could help me on this ,i want it start from one until 30 thanks
    Code:
    import java.util.Scanner;
    public class inte
    {
    	
        public static void main(String[] args)
        {
    	final int MAX_ROWW = 5;
    	final int MAX_STARR = 20;
    	
    	int i,k;
    
    	for(i=1; i <= 30 ;i++)
       	{
         
          		for(k=1; k <= MAX_ROWW; k++)
          		{
             
    		 
             		System.out.print(String.format ("%5d",i++));
          		}
          
          	System.out.println();
    
       	}
    
    
        }                 
        
    }
    the out come is as follow my question where is 6 ,10 ,
    why ?
    any help please
    1 2 3 4 5
    7 8 9 10 11
    13 14 15 16 17
    19 20 21 22 23
    25 26 27 28 29

  2. #2
    Join Date
    Feb 2004
    Posts
    541
    Code:
    System.out.print(String.format ("%5d",i++));
    You're incrementing the loop variable within the (nested) loop. Not a good thing to do as it can make the logic very hard to follow. Even worse in your case, it has led to an unwanted side effect. Every time the inner loop completes you are going to increment the outer loop variable twice - once in the code I have posted, and again in the outer loop control logic.

    I think you're going to have to rethink your algorithm for doing this. There's no need to have more than one loop, you can do it with one loop and have a variable that counts and determines when a new line is needed.

  3. #3
    Join Date
    Feb 2006
    Posts
    9

    here new code new problems

    Hi, I did as i you give me some hints but still i have some problem , the out come start from 0 even i want to start from 1.
    any help now please ?
    thanks

    Code:
    import java.util.Scanner;
    public class NestedPanels
    {
    	
        public static void main(String[] args)
        {
    	int[][] table = new int[20][5];
    	// Load the table with values
    		for (int row=0; row < table.length; row++)
    			for (int col=0; col < table[row].length; col++)
    
    				table[row][col] = row * 5 + col;
    
    			// Print the table
    			for (int row=0; row < table.length; row++)
    			{
    
    				for (int col=0; col < table[row].length; col++)
    				System.out.print (table[row][col] + "\t");
    				System.out.println();
    			}
    
        }                 
        
    }
    out come
    0 1 2 3 4
    5 6 7 8 9
    ....

Similar Threads

  1. Replies: 2
    Last Post: 03-16-2006, 07:39 PM
  2. For Loop Help
    By naazrael in forum Java
    Replies: 2
    Last Post: 12-14-2005, 11:22 AM
  3. Replies: 2
    Last Post: 08-25-2005, 03:00 PM
  4. Problem with a loop
    By salvinger in forum VB Classic
    Replies: 0
    Last Post: 05-07-2005, 02:38 PM
  5. Endless Loop
    By Mark in forum VB Classic
    Replies: 1
    Last Post: 03-23-2001, 04:41 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
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center