Results 1 to 3 of 3
  1. #1
    Quinn is offline Member
    Join Date
    May 2011
    Rep Power

    Default Drawing simple shapes using loops

    Hello all,
    I have an assignment that I'm working on where I need to draw simple shapes using asteriks (*)
    For example a square would be :
    * * *
    * * *
    * * *
    The length and width of the shape needs to be use inputed.
    So far I have the loop working to print the required number of * horizontally but I can't figure out how to make it drop a line and reprint the horizontal *'s repeatedly until I have an X by X square. I think I need to use a nested loop but I can't seem to get it to work.
    Here is what I have so far:
    Java Code:
    import javax.swing.JOptionPane;
    public class A2Q1
      public static void main(String[] args)
        String inputString;     //For reading input
        int sideLengthSquare;   //Length of side for square
        int height = 1;
        inputString = JOptionPane.showInputDialog("Enter the squares side length.");
        sideLengthSquare = Integer.parseInt(inputString);
        while (height <= sideLengthSquare)
            if (height <= sideLengthSquare)
            System.out.print ("* ");
                else System.out.print ("\n");

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    New Zealand
    Rep Power


    I think I need to use a nested loop
    Yes, you need to use nested loops rather than the single loop in the code you posted. Since you know how many times each loop will be repeated ahead of time (it's the number inputted by the user) for loops might be a good idea.

  3. #3
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Voorschoten, the Netherlands
    Blog Entries
    Rep Power


    May I suggest the following three parameter method: the first parameter c is the char to be printed, the second parameter n is the number of times c is to be printed, the last boolean parameter eol tells the method whether or not to print a new line at the end:

    Java Code:
    public static void printLine(char c, int n, boolean eol) {
       for (int i= 0; i < n; i++)
       if (eol)
    As an example, your square can be printed as follows:

    Java Code:
    for (int line= 0; line < 3; line++)
       printLine('*', 3, true);
    And many, many, more shapes can be printed by using a few loops and this little method.

    kind regards,

    Build a wall around Donald Trump; I'll pay for it.

Similar Threads

  1. Drawing shapes with for loops using characters
    By Strawlion in forum New To Java
    Replies: 3
    Last Post: 02-16-2011, 09:32 PM
  2. drawing shapes in java help
    By alphajoseph in forum Java 2D
    Replies: 2
    Last Post: 09-29-2009, 07:35 PM
  3. simple if statements and loops
    By merfen58 in forum New To Java
    Replies: 6
    Last Post: 09-22-2009, 10:46 AM
  4. Help with ASCII Drawing--For loops--if/else
    By in forum New To Java
    Replies: 1
    Last Post: 09-13-2009, 01:31 AM
  5. Getting User Input to Drawing Shapes
    By mlfatty in forum AWT / Swing
    Replies: 2
    Last Post: 03-04-2009, 07:36 AM

Posting Permissions

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