Results 1 to 16 of 16
  1. #1
    luke is offline Member
    Join Date
    Sep 2010
    Posts
    62
    Rep Power
    0

    Question Displaying a square based on user's input

    Hello,
    I have to write a program that displays hollow square with asterisks or plus sign or any sign. The sign is not important.
    The size of the square is entered by the user. For example, when the user enters 1 the program displays
    +++
    + +
    +++
    when enters 2 it displays something like this
    ++++
    + +
    + +
    ++++
    and so on.Up until size 20. Basically it has to be a square, similar to the 8-bit figures displayed in DOS with asterisks or plus sign and with every bigger number inputted by the user it has to expand.

    I coded up to size 2:

    Java Code:
    import java.util.Scanner;
    
    public class Square 
    {
    
    
        public static void main( String[] args ) 
        {
            String str = "+";
            int x;
            Scanner input = new Scanner(System.in);
            
            for(int a = 1; a <= 21; a++)
            {
            System.out.print("\nEnter size of the side: ");
            x = input.nextInt();
            
            if( x==1 )
            {
                System.out.printf("%s%s%s", str, str, str);
                System.out.printf("\n%s %s",str, str);
                System.out.printf("\n%s%s%s",str,str,str);
            }
            else if( x==2 )
            {
                System.out.printf("%s%s%s%s",str,str,str,str);
                System.out.printf("\n%s  %s",str,str);
                System.out.printf("\n%s  %s",str,str);
                System.out.printf("\n%s%s%s%s",str,str,str,str);
            }
    
        and so on
    
    }
    Is there any other way of doing this because if I keep going this way the code will become just crazy huge and full of System.out.printf methods?

  2. #2
    collin389 is offline Senior Member
    Join Date
    Nov 2009
    Posts
    235
    Rep Power
    6

    Default

    take a look at for loops: For Loop Tutorial

  3. #3
    luke is offline Member
    Join Date
    Sep 2010
    Posts
    62
    Rep Power
    0

    Default

    Thank you for your reply, collin389.
    Last edited by luke; 09-26-2010 at 05:52 AM.

  4. #4
    luke is offline Member
    Join Date
    Sep 2010
    Posts
    62
    Rep Power
    0

    Default

    OK, using your advice I have changed the two if statements to

    Java Code:
    if( x==1 )
    	 {
    	                for( int i=1; i < 3; i++ )
    			{
    				System.out.println("" + s +s );
    			}			
    
    	 }
    	 else if( x==2 )
    	 {
    		for( int i=1; i < 4; i++ )
    		{
    			System.out.println("" +s +s +s );
    		}
             }
    and this is just until size two. I am trying to incorporate an empty string in the second line of the square in order to get * *, but I am always getting ***.
    So if x==2 I am getting
    ***
    ***
    ***
    Any ideas?

  5. #5
    Zack's Avatar
    Zack is offline Senior Member
    Join Date
    Jun 2010
    Location
    Destiny Islands
    Posts
    692
    Rep Power
    5

    Default

    That's because you are adding the same string instead of a space.

    Realize this:
    Java Code:
    String x = "+";
    String y = x+x+x;
    Here, y is the string x (which is a + sign) repeated over and over--hence, y == "+++".

    If, instead, you wanted "+ +", you would do something like this...
    Java Code:
    String x = "+";
    String y = x+" "+x;

    In your case, also, you should realize that you don't actually need two if statements--notice that in each for loop, your constraining variable (i < ...) is equal to x+2.
    Last edited by Zack; 09-26-2010 at 07:43 AM.

  6. #6
    luke is offline Member
    Join Date
    Sep 2010
    Posts
    62
    Rep Power
    0

    Default

    Quote Originally Posted by Zack View Post
    That's because you are adding the same string instead of a space.

    Realize this:
    Java Code:
    String x = "+";
    String y = x+x+x;
    Here, y is the string x (which is a + sign) repeated over and over--hence, y == "+++".

    If, instead, you wanted "+ +", you would do something like this...
    Java Code:
    String x = "+";
    String y = x+" "+x;

    In your case, also, you should realize that you don't actually need two if statements--notice that in each for loop, your constraining variable (i < ...) is equal to x+2.
    Thanks for your reply, Zack.
    But if I do this
    Java Code:
    String x = "+";
    String y = x+" "+x;
    I will be getting
    + +
    + +
    + +

    while I need (for square size 2)
    +++
    + +
    +++

    and for square size 3 it has to be
    ++++
    + +
    + +
    ++++

    and so on. To simplify - the number of "+" signs in the sides of the square corresponds to the integer, inputted by the user.

  7. #7
    Zack's Avatar
    Zack is offline Senior Member
    Join Date
    Jun 2010
    Location
    Destiny Islands
    Posts
    692
    Rep Power
    5

    Default

    Then your for loop won't handle everything. Think of it like this:
    Java Code:
    // x represents the width of the square, so 2 == ++ at the top.
    print +, x times;
    repeat this x-2 times:
         print +, then a space x-2 times, then a +
    print +, x times;

    Hopefully that helps you think a little bit--the for loop should only handle the "repeat" portion of the code, whereas the other parts should be procedural on their own.

    I'm off to bed now, so hopefully this can get you going at the very least--if not, I'm sure someone else can help you out before I'm back tomorrow. Good luck!

  8. #8
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,729
    Blog Entries
    7
    Rep Power
    21

    Default

    Have a look at this little method:

    Java Code:
    void line(char c, char m, int n) {
       System.out.print(c);
       for (int i= 0; i < n; i++)
          System.out.print(m);
       System.out.println(c);
    }
    It prints a single character 'c' followed by n characters 'm' and one 'c' again; the method also prints a new line at the end. You can build squares with it ...

    kind regards,

    Jos

  9. #9
    luke is offline Member
    Join Date
    Sep 2010
    Posts
    62
    Rep Power
    0

    Default

    Thanks for your kind replies, Zack and JosAH. I have just got out of bed.
    I'll try your advise and will be back.

  10. #10
    luke is offline Member
    Join Date
    Sep 2010
    Posts
    62
    Rep Power
    0

    Default

    Thanks to everybody for the help. It was really valuable.

  11. #11
    luke is offline Member
    Join Date
    Sep 2010
    Posts
    62
    Rep Power
    0

    Default

    It's about the same stupid square. I realized that actually instead of printing a square it gives me a rectangular. It freaks me out because I don't know what to do. I tried many things. You can take a look at the picture what is going on.


    The code is
    Java Code:
    public static void main( String args[] ) {
        
            Scanner inp = new Scanner(System.in);
            System.out.print("Enter number: ");
            int x = inp.nextInt();
    
            char s = '*', e = ' ';
    
    
            if( x == 4 ) {
    
                    for( int y=1; y < 5; y++ )
    
                        System.out.print(s);
    
                    System.out.println();
    
                    for( int a=1; a < 3; a++ ) { 
                                        
                        System.out.print(s);
    
                        for( int i=1; i < 3; i++ )
                    
                            System.out.print(e);
                    
                        System.out.print(s);
                        System.out.println();    
                    }
                    
                    for( int y=1; y < 5; y++ )
    
                        System.out.print(s);
                        
                
        }
    
    
    
    
        }

  12. #12
    luke is offline Member
    Join Date
    Sep 2010
    Posts
    62
    Rep Power
    0

    Default

    Nobody knows how to make it real square?

  13. #13
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,812
    Rep Power
    25

    Default

    What is a real square?
    What you printed had 4 sides with the same number of elements.
    To get a "visually square" shape, you need to find a character that takes the same amount of space horizontally as the vertical spacing between lines printed on the console.

  14. #14
    luke is offline Member
    Join Date
    Sep 2010
    Posts
    62
    Rep Power
    0

    Default

    Thanks for your reply, Norm.
    I thought I was printing an extra horizontal line.
    I noticed when I press Alt+Enter in order to get the Command Prompt window maximized the square is a "square". But when the console window is minimized it creates the impression that there is an extra horizontal line.

  15. #15
    bojangle is offline Member
    Join Date
    Jul 2012
    Posts
    1
    Rep Power
    0

    Default Re: Displaying a square based on user's input

    Quote Originally Posted by luke View Post
    Nobody knows how to make it real square?
    public

    If you want to make it look and feel more semetrical. Then you can just add a space behind the asterisk and add a double space to the interior.
    You can do the same with the above for loop example as well.

    Displaying a square based on user's input-screenshot-2012-07-13-21-42-18.png

    Java Code:
     public static void main( String args[] ) {
           Scanner input = new Scanner(System.in);
    
           int side, width;
            
            System.out.print("This program prints an asterisk square!");
            System.out.println();
            System.out.print("Enter a side length: ");
            side = input.nextInt();
         
            int hight = side;
    
            if( side <= 20 && side > 0) {
                
                while(height >= 1){
                    width = 1;
                    
                    while(width <= side){  
                        System.out.print("* "); //There is an asterisk and a space here
                        width++;
                        while(height >=2 && height <= side-1 && width <= side-1){
                            System.out.print("  "); //There's two spaces here
                            width++;
                        }
                    }
                    
                   --height;
                   System.out.println();
                }    
            }else{
                System.out.print("Out of bounds!");
            }
            System.out.println();
        }
    Attached Thumbnails Attached Thumbnails Displaying a square based on user's input-screenshot-2012-07-13-21-32-53.png  
    Last edited by bojangle; 07-14-2012 at 07:49 AM. Reason: remove an image

  16. #16
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,729
    Blog Entries
    7
    Rep Power
    21

    Default Re: Displaying a square based on user's input

    This thread is almost two years old; I'm closing it.

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Displaying Multiple Tables based on Input
    By Berzerk in forum New To Java
    Replies: 11
    Last Post: 06-17-2010, 08:54 AM
  2. Replies: 3
    Last Post: 10-28-2009, 02:45 AM
  3. Reading data from csv file based on specific input
    By jaiminparikh in forum Advanced Java
    Replies: 14
    Last Post: 02-13-2009, 10:07 PM
  4. Perfect Square Array Input Using For Loop
    By dalangley in forum New To Java
    Replies: 9
    Last Post: 01-27-2009, 02:33 AM
  5. Replies: 1
    Last Post: 01-23-2009, 05:13 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
  •