Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 11 of 11

Thread: Help with making a custom checkerboard with nested loop

  1. #1
    Junior Member
    Join Date
    Feb 2014
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Help with making a custom checkerboard with nested loop

    Hello, I'm trying to make a custom checkboard with given user input, in the form of (# of rows, # of columns, size of each square, filler character).
    So the input 2 3 5 * would look like
    *****.........*****
    *****.........*****
    *****.........*****
    *****.........*****
    *****.........*****
    .........*****
    .........*****
    .........*****
    .........*****
    .........*****
    I've made my loop, but I am unable to get more then a 1 1 1 checkerboard properly.
    I am stuck on how to divide the filler characters to make the proper square size. As of now they are all one lined.

    import java.util.*;
     
    public class Checker{ 
     
    public static void main(String[] args) {
    int col, row, size; char filler;
    System.out.println("Please enter 3 numbers and a character."); //output
    Scanner scan = new Scanner(System.in); //input
     
    row = scan.nextInt();
    col = scan.nextInt();
    size = scan.nextInt();
    filler = scan.next().charAt(0);  // defined variables
     
    int r,c,i=0,j=0,k=0;
     
    for (r=1; r<=row; r++)
        {for (c=1; c<=col; c++)
            {do
     
                {   if ((r % 2 != 0 && c % 2 !=0) || (r %2 == 0 && c % 2 == 0))
                            { do {
                                System.out.print(filler);
                                i++;}
                                while (i<size);
                            	i = 0;
     
                            	}
     
     
                    else { 
                              do
                              {  System.out.print(" ");
                                  j++;}
                                  while (j<size);
                                  j = 0;
                         }
                k++;
                }while (k<size); 
            	k = 0;
     
            }System.out.println("\n");}
    System.out.println("\nHave a nice day. Goodbye.");
     
    }
    }


  2. #2
    Member andbin's Avatar
    Join Date
    Dec 2013
    Location
    Italy
    Posts
    443
    Thanks
    4
    Thanked 122 Times in 114 Posts

    Default Re: Help with making a custom checkerboard with nested loop

    Quote Originally Posted by MonroviaLiberia View Post
    int r,c,i=0,j=0,k=0;
     
    for (r=1; r<=row; r++)
        {for (c=1; c<=col; c++)
            {do
     
                {   if ((r % 2 != 0 && c % 2 !=0) || (r %2 == 0 && c % 2 == 0))
                            { do {
                                System.out.print(filler);
                                i++;}
                                while (i<size);
                            	i = 0;
     
                            	}
     
     
                    else { 
                              do
                              {  System.out.print(" ");
                                  j++;}
                                  while (j<size);
                                  j = 0;
                         }
                k++;
                }while (k<size); 
            	k = 0;
     
            }System.out.println("\n");}
    Are you sure you need all this long/complex code?

    Hint: just 4 nested for are sufficient, with 1 System.out.print (for filler or space decided using the ?: operator) and 1 System.out.println().
    Andrea, www.andbin.netSCJP 5 (91%) – SCWCD 5 (94%)

    Useful links for Java beginnersMy new project Java Examples on Google Code

  3. #3
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Help with making a custom checkerboard with nested loop

    You're also missing some simple math required to make the loops work. Your specification is a little confusing, but if you think it through, then the user's input

    2 3 5 *

    is a 2 x 3 checkerboard (row, column)
    with a 5 x 5 sized square

    So the number of rows is actually 2 squares * 5 rows / square or 10 rows. Then you'll have to divide those rows into 'blocks' so that the correct character is printed.

    You'll need to do a similar calculation for the number of real columns.

  4. #4
    Member andbin's Avatar
    Join Date
    Dec 2013
    Location
    Italy
    Posts
    443
    Thanks
    4
    Thanked 122 Times in 114 Posts

    Default Re: Help with making a custom checkerboard with nested loop

    As GregBrannon has pointed out (and I did not think immediately), it's also possible to do this with just 2 for nested loops and a bit of added math.
    However my initial think about 4 nested for is also good because it expresses the intentions more clearly and without much math.
    Andrea, www.andbin.netSCJP 5 (91%) – SCWCD 5 (94%)

    Useful links for Java beginnersMy new project Java Examples on Google Code

  5. #5
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Help with making a custom checkerboard with nested loop

    @andbin: Now I understand why you suggested 4 loops. I was trying to figure out why so many were needed, but I was stuck in my own design idea, and I love the math.

  6. #6
    Member
    Join Date
    Oct 2013
    Location
    United Kingdom
    Posts
    62
    Thanks
    1
    Thanked 4 Times in 4 Posts

    Default Re: Help with making a custom checkerboard with nested loop

    @GregBrannon and @andbin: 2 nested for-loops can be nice to display the output. But I feel that will be difficult in printing correct character in correct place. I am running through some ideas where we can do this in 2 nested for-loops. I tried a few and I am still trying, but any suggestions would be much appreciated.
    Thanks and regards,
    Sambit Swain

  7. #7
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Help with making a custom checkerboard with nested loop

    Since I suggested it . . .

    My approach:

    - The size of the square defines the height or number of rows in a 'block' of output
    - Each row in a block of output begins with the same character, either space for a 'white' square or the user's chosen character for a 'black' sqaure
    - The block character being output alternates at the column block boundary

    Logic (roughly)
    Start the row with the correct character (outer loop)
    Swap the character at each column block boundary (inner loop)
    Swap the row character at the row block boundary

    I've coded it, but it's not as efficient as I'd like it to be. In the end, the 4-loop approach may be inherently more efficient, but I haven't coded that one.

  8. #8
    Member
    Join Date
    Oct 2013
    Location
    United Kingdom
    Posts
    62
    Thanks
    1
    Thanked 4 Times in 4 Posts

    Default Re: Help with making a custom checkerboard with nested loop

    @GregBrannon: Finally your approach was a success, but it came with lots of code. Thanks, learnt a different way
    Thanks and regards,
    Sambit Swain

  9. #9
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Help with making a custom checkerboard with nested loop

    Really? Here's my for loop. Not that bad . . .
            for ( int i = 0 ; i < rows * squareSize ; i++ )
            {
                // start every row in the block with the same character
                if ( i % squareSize == 0 )
                {
                    swapStartChar();
                }
     
                printChar = startChar;
     
                for ( int j = 0 ; j < columns * squareSize ; j++ )
                {
                    if ( j % squareSize == 0 )
                    {
                        swapPrintChar();
                    }
     
                    System.out.print( printChar );
                }
     
                System.out.println();
     
            }

  10. The Following User Says Thank You to GregBrannon For This Useful Post:

    Sambit (February 7th, 2014)

  11. #10
    Junior Member
    Join Date
    Feb 2014
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help with making a custom checkerboard with nested loop

    I've fixed my loops, but still am unsure about how to go about the math. I get the general calculations, but I could do with some pointers on how and where to implement them. Thanks

  12. #11
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Help with making a custom checkerboard with nested loop

    Show what you done and describe what help you need. Post a sample run, too.

Similar Threads

  1. Loop & custom exception error Please help
    By dpjmie in forum What's Wrong With My Code?
    Replies: 8
    Last Post: April 18th, 2013, 04:40 PM
  2. Need help with this nested loop
    By beerye28 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: January 22nd, 2013, 09:51 PM
  3. Please Help: Need Help with my Nested Loop
    By hiroprotagonist in forum Loops & Control Statements
    Replies: 1
    Last Post: November 24th, 2012, 10:43 AM
  4. Nested For Loop!
    By samadniz in forum Object Oriented Programming
    Replies: 3
    Last Post: September 3rd, 2012, 04:03 PM
  5. Making custom search engine
    By bochra in forum Web Frameworks
    Replies: 4
    Last Post: June 20th, 2011, 07:37 AM