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

Thread: URgh, drawing a square

  1. #1
    Junior Member
    Join Date
    Oct 2011
    Posts
    27
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default URgh, drawing a square

    My current task is to draw a square and im nowhere near into it. Been working on the project now for about 4 hours and im just confused. Heres my code, atm i can't even get it to do what i want it to do.

     
     
    public class Main
    {
        public static void main(String args[])
        {
            System.out.print("#Number Of Stars: ");
            int sizeofsquare = 5;
            int line = 1;
            int down = 1;
            int across = 1;
            System.out.println();
     
            while ( line <= sizeofsquare ){
     
                System.out.print("*");
                line = line + 1;
     
            }
     
            while ( down < sizeofsquare ){
     
                down = down + 1;
                System.out.println();
     
                while ( across <= sizeofsquare ){
     
                    System.out.print("*");
                    across = across + 1;
     
                }
     
            }
     
     
        }
    }

    Basically im trying to draw a square out of stars after having the user read in how many stars will be along the y and x.

    for example if i read in 5 the square would be:

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

    and would change depending on the number i entered, the max number of stars being 40 along the x/y axis.

    So far ive managed to get *'s to constantly generate after reading a number and have no idea why they don't stop when the count gets to the number that has been entered.
    Last edited by BITmixit; October 25th, 2011 at 11:53 AM.


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,972
    My Mood
    Hungover
    Thanks
    133
    Thanked 555 Times in 476 Posts

    Default Re: URgh, drawing a square

    If you want help, you'll have to provide an SSCCE that demonstrates the problem. You're close, but what is BIO? I assume it's a Scanner?

    Also, I'd recommend stepping through this with a debugger, or at least adding print lines that let you know what's going on. For example, what is the value of sizeofsquare each iteration through the loop? When will it be > 40 to allow the loop to exit?
    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!

  3. #3
    Junior Member
    Join Date
    Oct 2011
    Posts
    27
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: URgh, drawing a square

     
     
    public class Main
    {
        public static void main(String args[])
        {
            System.out.print("#Number Of Stars: ");
            int sizeofsquare = 5;
            int down = 1;
            int across = 2;
            System.out.println();
     
            while ( down <= sizeofsquare ){
     
                System.out.print("*");
     
                while ( across <= sizeofsquare ){
     
                    System.out.print("*");
                    across = across + 1;
     
                }
     
                System.out.println();
     
                down = down + 1;
     
            }
     
        }
    }

    Right i've managed to shrink it abit but at the moment i get:

    *****
    *
    *
    *
    *

    when i need:

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

    I don't really understand why the while loop containing the across * stops either :S
    Last edited by BITmixit; October 25th, 2011 at 12:12 PM.

  4. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,972
    My Mood
    Hungover
    Thanks
    133
    Thanked 555 Times in 476 Posts

    Default Re: URgh, drawing a square

    Again, I'd recommend stepping through this with a debugger or at least adding print lines to understand what's going on. Pay special attention to the value of across each time through the loops.
    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!

  5. #5
    Junior Member
    Join Date
    Oct 2011
    Posts
    27
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: URgh, drawing a square

    I used the debugger and from what it showed me the while across loop is doing its job then thinking its done and letting the other loop then do its job.

    However ive tried to move the across=across + 1 and it just makes the across while loop continue forever.

    Do i need to add another while loop to the mix?

  6. #6
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,972
    My Mood
    Hungover
    Thanks
    133
    Thanked 555 Times in 476 Posts

    Default Re: URgh, drawing a square

    Quote Originally Posted by BITmixit View Post
    I used the debugger and from what it showed me the while across loop is doing its job then thinking its done and letting the other loop then do its job.
    I'm not sure you understand how the loops are working. One is inside the other, they aren't happening one after another.

    Quote Originally Posted by BITmixit View Post
    However ive tried to move the across=across + 1 and it just makes the across while loop continue forever.
    What did you expect that to accomplish?

    Quote Originally Posted by BITmixit View Post
    Do i need to add another while loop to the mix?
    What do you expect that to accomplish?

    I suggest you slow down and take a closer look at what your existing program is doing before you attempt to add anything to it.

    Another approach would be to write a method that prints a single row of a certain parameterized length, then call that method the correct number of times.

    Also, why are you using while loops instead of for loops?
    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!

  7. #7
    Junior Member
    Join Date
    Oct 2011
    Posts
    27
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: URgh, drawing a square

    Fixed it and its working now, took a closer look then sort of had a duh moment and realised i could reset the across variable outside of the across loop thus resetting it till the down loop was done.

    Have submitted it to the uni server and its correct

Similar Threads

  1. HW Accelerated Square Root
    By tomster in forum Java SE APIs
    Replies: 2
    Last Post: October 3rd, 2011, 05:12 AM
  2. triad square
    By mistu in forum Member Introductions
    Replies: 2
    Last Post: August 22nd, 2011, 08:13 AM
  3. Magic Square Checker
    By Hypnos in forum Object Oriented Programming
    Replies: 1
    Last Post: April 3rd, 2011, 06:37 PM
  4. Square root not working
    By sp11k3t3ht3rd in forum What's Wrong With My Code?
    Replies: 2
    Last Post: January 7th, 2011, 05:25 PM
  5. Finding square root
    By Tracy22 in forum The Cafe
    Replies: 1
    Last Post: October 18th, 2010, 06:18 PM