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 19 of 19

Thread: Checkerboard (asterisk pattern) problem

  1. #1
    Junior Member
    Join Date
    Jun 2012
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Talking Checkerboard (asterisk pattern) problem

    Hi guys, this is strictly NOT homework but my own home study. Using just While loops, I managed to create an asterisk checkerboard with 8 asterisk columns and rows usinhg my own code below:

    public class Exe432 
    {
        public static void main(String[] args) 
        {
            int line_number = 1;
            int asterisk_amount = 1;
     
            while ( line_number <= 8 )
            {
                while ( asterisk_amount <= 8 )
                {
                    System.out.print( "* " );
                    asterisk_amount += 1;
                }
                asterisk_amount = 1;
                System.out.println();
                line_number += 1;
            }
        }
    }

    That's cool, but I need the checkerboard to have indented rows on some exactly as such below:

    * * * * * * * *
    * * * * * * * *
    * * * * * * * *
    * * * * * * * *
    * * * * * * * *
    * * * * * * * *
    * * * * * * * *
    * * * * * * * *

    Such a small problem but I'm finding it irritatingly difficult to make that one tweak. Added code to the one I already provided above is what I'd prefer thanks


  2. #2
    Junior Member
    Join Date
    Jun 2012
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Checkerboard (asterisk pattern) problem

    Sorry guys, correction from last post. This is the checkerboard I need output from the code. Thanks.

    * * * * * * * *
     * * * * * * * *
    * * * * * * * *
     * * * * * * * *
    * * * * * * * *
     * * * * * * * *
    * * * * * * * *
     * * * * * * * *

  3. #3
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Checkerboard (asterisk pattern) problem

    How did you make it happen when you typed it out? Do that in the code.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  4. #4
    Junior Member
    Join Date
    Jun 2012
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Checkerboard (asterisk pattern) problem

    Quote Originally Posted by KevinWorkman View Post
    How did you make it happen when you typed it out? Do that in the code.
    Hi Kevin. I'm not sure what you mean sorry.

  5. #5
    Member
    Join Date
    Jul 2012
    Posts
    119
    Thanks
    0
    Thanked 19 Times in 19 Posts

    Default Re: Checkerboard (asterisk pattern) problem

    Quote Originally Posted by jjb1989 View Post
    Hi guys, this is strictly NOT homework but my own home study. Using just While loops, I managed to create an asterisk checkerboard with 8 asterisk columns and rows usinhg my own code below:

    public class Exe432 
    {
        public static void main(String[] args) 
        {
            int line_number = 1;
            int asterisk_amount = 1;
     
            while ( line_number <= 8 )
            {
                while ( asterisk_amount <= 8 )
                {
                    System.out.print( "* " );
                    asterisk_amount += 1;
                }
                asterisk_amount = 1;
                System.out.println();
                line_number += 1;
            }
        }
    }

    That's cool, but I need the checkerboard to have indented rows on some exactly as such below:

    * * * * * * * *
    * * * * * * * *
    * * * * * * * *
    * * * * * * * *
    * * * * * * * *
    * * * * * * * *
    * * * * * * * *
    * * * * * * * *

    Such a small problem but I'm finding it irritatingly difficult to make that one tweak. Added code to the one I already provided above is what I'd prefer thanks
    insert the "\n" as following
     
    public class Exe432 
    {
        public static void main(String[] args) 
        {
            int line_number = 1;
            int asterisk_amount = 1;
     
            while ( line_number <= 8 )
            {
                while ( asterisk_amount <= 8 )
                {
                    System.out.print( "* " );
                    asterisk_amount += 1;
                }
                asterisk_amount = 1;
                System.out.println("\n");
                line_number += 1;
            }
        }
    }

  6. #6
    Junior Member
    Join Date
    Jun 2012
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Checkerboard (asterisk pattern) problem

    Quote Originally Posted by Voodoo View Post
    insert the "\n" as following
     
    public class Exe432 
    {
        public static void main(String[] args) 
        {
            int line_number = 1;
            int asterisk_amount = 1;
     
            while ( line_number <= 8 )
            {
                while ( asterisk_amount <= 8 )
                {
                    System.out.print( "* " );
                    asterisk_amount += 1;
                }
                asterisk_amount = 1;
                System.out.println("\n");
                line_number += 1;
            }
        }
    }
    Thanks for your reply Voodoo. I'm afraid your tweak didn't produce the output I was looking for. Just to make myself more clearer, below is what I want my code to output:

    * * * * * * * *
     * * * * * * * *
    * * * * * * * *
     * * * * * * * *
    * * * * * * * *
     * * * * * * * *
    * * * * * * * *
     * * * * * * * *

  7. #7
    Member
    Join Date
    Jul 2012
    Posts
    119
    Thanks
    0
    Thanked 19 Times in 19 Posts

    Default Re: Checkerboard (asterisk pattern) problem

    Hey, what is your OS? On my PC (Windows Vista and Ubunto) it works! Have you this statement System.out.println("\n");?

  8. #8
    Member
    Join Date
    Jul 2012
    Posts
    119
    Thanks
    0
    Thanked 19 Times in 19 Posts

    Default Re: Checkerboard (asterisk pattern) problem

    sorry UBUNTU not ubunto

  9. #9
    Member
    Join Date
    Jul 2012
    Posts
    119
    Thanks
    0
    Thanked 19 Times in 19 Posts

    Default Re: Checkerboard (asterisk pattern) problem

    ...by the way, try this:
    [

  10. #10
    Member
    Join Date
    Jul 2012
    Posts
    119
    Thanks
    0
    Thanked 19 Times in 19 Posts

    Default Re: Checkerboard (asterisk pattern) problem

    by the way try this:
    ...
     
            while ( line_number <= 8 )
            {
                String tmp = "";
                while ( asterisk_amount <= 8 )
                {
                    tmp += "* ";
                    asterisk_amount += 1;
                }
                asterisk_amount = 1;
                System.out.println(tmp+"\n");
                line_number += 1;
    ...
    it's faster and less IO-overhead

  11. #11
    Member
    Join Date
    Jul 2012
    Posts
    119
    Thanks
    0
    Thanked 19 Times in 19 Posts

    Default Re: Checkerboard (asterisk pattern) problem

    oh...i misread your posting, terribly sorry. Here is the tweak:
     
            while ( line_number <= 8 )
            {
                String tmp = "";
                if ((line_number & 1) == 0) tmp = " ";
                while ( asterisk_amount <= 8 )
                {
                    tmp += "* ";
                    asterisk_amount += 1;
                }
                asterisk_amount = 1;
                System.out.println(tmp+"\n");
                line_number += 1;
            }
    ..
    Again, I need new glasses

  12. #12
    Junior Member
    Join Date
    Jun 2012
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Smile Re: Checkerboard (asterisk pattern) problem

    Quote Originally Posted by Voodoo View Post
    oh...i misread your posting, terribly sorry. Here is the tweak:
     
            while ( line_number <= 8 )
            {
                String tmp = "";
                if ((line_number & 1) == 0) tmp = " ";
                while ( asterisk_amount <= 8 )
                {
                    tmp += "* ";
                    asterisk_amount += 1;
                }
                asterisk_amount = 1;
                System.out.println(tmp+"\n");
                line_number += 1;
            }
    ..
    Again, I need new glasses
    Thanks again Voodoo lol. My OS is Windows 7. Your code did the trick thanks. If I can be just a slight more picky on the code, would you be kind enough to answer me the following:

    I quote from my exercise book: Write an application that uses only the output statements

    System.out.print( "* " );
    System.out.print( " " );
    System.out.println();

    and While iteration only to produce checkerboard.

    Sorry to be picky, but I'd like to keep to the exercise's demands so I don't drift off to any more advanced or more complex code than is required from the chapter. Thanks again!

  13. #13
    Member
    Join Date
    Jul 2012
    Posts
    119
    Thanks
    0
    Thanked 19 Times in 19 Posts

    Default Re: Checkerboard (asterisk pattern) problem

    Hi jjb1989
    System.out.print() or System.out.println() is the invocation of the the method print() or println() of the Class PrintStream PrintStream (Java 2 Platform SE v1.4.2)
    the print() method just prints as it is, the println() always terminates the line. If you use print() you have to care about the NewLine or so and, very important, to "flush" the "remaining" in the output buffer with flush(). Otherwise you may get an incomplete result (e.g. missing some characters in your printout) when your program terminates.
    System.out.print( "* " ); write/print a string of an asterisk + a blank into the output buffer
    System.out.print( " " ); write/print a string of "1 blank" into the output buffer
    System.out.println(); insert a line-separator into output buffer and "flushes" the buffer out.
    Okay?

  14. #14
    Junior Member
    Join Date
    Jun 2012
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Checkerboard (asterisk pattern) problem

    Quote Originally Posted by Voodoo View Post
    Hi jjb1989
    System.out.print() or System.out.println() is the invocation of the the method print() or println() of the Class PrintStream PrintStream (Java 2 Platform SE v1.4.2)
    the print() method just prints as it is, the println() always terminates the line. If you use print() you have to care about the NewLine or so and, very important, to "flush" the "remaining" in the output buffer with flush(). Otherwise you may get an incomplete result (e.g. missing some characters in your printout) when your program terminates.
    System.out.print( "* " ); write/print a string of an asterisk + a blank into the output buffer
    System.out.print( " " ); write/print a string of "1 blank" into the output buffer
    System.out.println(); insert a line-separator into output buffer and "flushes" the buffer out.
    Okay?
    Thanks for the information Voodoo. I'm aware of what each of the methods do but have trouble using them and repetition to produce that checkerboard. I'm missing something and I just want to see the whole code as asked for by the exercise. Thanks again!

  15. #15
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Checkerboard (asterisk pattern) problem

    I will for now refrain from giving you the answer, but a nested for loop, and the % operator would be able to stagger the starting character, * or 'space', on staggered lines.

    but I'd like to keep to the exercise's demands
    sorry, I missed that post..
    If you think about what you intend to write one line at a time, one character at a time:

    First line, first character, print("* "), second character print ("* ")
    second line, first character, print(" "), second character print("* ")

    Every other line you want to print a space before your series of "* ". Remember one loop is per line and one is per entry on the current line in the making.
    Last edited by jps; July 15th, 2012 at 01:16 PM.

  16. #16
    Junior Member
    Join Date
    Jun 2012
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Checkerboard (asterisk pattern) problem

    Quote Originally Posted by jps View Post
    I will for now refrain from giving you the answer, but a nested for loop, and the % operator would be able to stagger the starting character, * or 'space', on staggered lines.


    sorry, I missed that post..
    If you think about what you intend to write one line at a time, one character at a time:

    First line, first character, print("* "), second character print ("* ")
    second line, first character, print(" "), second character print("* ")

    Every other line you want to print a space before your series of "* ". Remember one loop is per line and one is per entry on the current line in the making.
    Thanks jps, finally solved it! Thanks to Voodoo as well!

  17. #17
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Checkerboard (asterisk pattern) problem

    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  18. #18
    Junior Member
    Join Date
    Sep 2013
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Checkerboard (asterisk pattern) problem

    Use a nested for loop to get the pattern you need.
    public class StarTriangle
    {
    ...
    }
    Last edited by copeg; September 9th, 2013 at 12:45 PM. Reason: Removed spoonfeeding

  19. #19
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Checkerboard (asterisk pattern) problem

    System.our.println("_Joe"
    Read post #17, and be sure to check dates of threads before posting

Similar Threads

  1. Help with Checkerboard GraphicsProgram (Due Tomorrow!)
    By strengthonor in forum What's Wrong With My Code?
    Replies: 3
    Last Post: September 16th, 2011, 09:40 AM
  2. Please help! Nested while loops and asterisk triangles!
    By rockout341 in forum Loops & Control Statements
    Replies: 12
    Last Post: September 15th, 2011, 11:50 AM
  3. Regular Expression pattern - complex pattern syntax
    By SmartAndy in forum Algorithms & Recursion
    Replies: 3
    Last Post: June 7th, 2011, 04:40 AM
  4. [SOLVED] Grid Pattern problem:
    By mick.bhattarai in forum What's Wrong With My Code?
    Replies: 4
    Last Post: December 30th, 2010, 07:45 AM
  5. asterisk forming diamond
    By cutee_eyeh in forum What's Wrong With My Code?
    Replies: 6
    Last Post: October 13th, 2010, 08:53 PM