Disclosure: When you purchase a service or a product through our links, we sometimes earn a commission.

How to Create a Hollow Rectangle Using Asterisks in Java

January 8, 2018

A hollow rectangle such as the box shown below can be drawn by printing the star symbol on only the first and the last row, and the first and the last column.

We can fill the rest of the spaces, i.e. from row two up to the (last row – 1) and from column two up to the (last column – 1) with spaces to produce the hollow rectangle.

Given the variable ‘size’ as the size of the row and the column: we can write the logic as follows:


for (int i=1; i<=size; i++){
   for(int j = 1; j<=size; j++){
       if(i==1 || i == size || j == 1 || j == size){
         System.out.print("*");
    }else {
      System.out.print(" ");
   }
 }
 System.out.println(); //Jump to the next row
 }

Here is the full code:


/** This program prints a hollow box using  the asterisk symbol (*) using
  * the size given by the user
**/
import java.util.*; //We need to import it to use the Scanner class
public class HollowBox{
    public static void hollowBox(int size){
	for (int i=1; i<=size; i++){
	   for(int j = 1; j<=size; j++){
	       if(i==1 || i == size || j == 1 || j == size){
		  System.out.print("*");
		}else {
		    System.out.print(" ");
		}
	    }
	    System.out.println(); //Jump to the next row
	}
   }
   public static void main(String[] args) {
      Scanner input = new Scanner(System.in);
      System.out.println("Please enter a size");
      int size = input.nextInt();
      hollowBox(size);
  }
}

5 comments on “How to Create a Hollow Rectangle Using Asterisks in Java

  1. Resources these as the 1 you mentioned right here will be incredibly useful to myself! I will publish a hyperlink to this web page on my personal blog. I am certain my site guests will discover that fairly helpful.

  2. A lot of thanks for each of your hard work on this web page. Kim really likes going through investigation and it’s really easy to understand why. Many of us know all of the dynamic method you make powerful strategies on this web blog and as well as improve participation from people on that issue so our girl is in fact learning a lot of things. Have fun with the remaining portion of the new year. You are conducting a pretty cool job.

  3. I’m also commenting to let you be aware of of the brilliant discovery my child encountered checking the blog. She came to understand a lot of details, including how it is like to have a marvelous coaching character to get certain people completely comprehend a variety of complex issues. You actually did more than her desires. I appreciate you for coming up with such essential, dependable, edifying and cool guidance on that topic to Ethel.

  4. I happen to be writing to make you understand what a terrific experience our princess went through visiting yuor web blog. She realized such a lot of things, with the inclusion of what it is like to possess an incredible teaching style to make the others without hassle thoroughly grasp various tricky topics. You undoubtedly surpassed visitors’ expected results. Thanks for showing those warm and friendly, trustworthy, revealing not to mention unique tips on your topic to Sandra.

  5. Thanks for the suggestions about credit repair on this particular web-site. The things i would offer as advice to people is usually to give up this mentality that they buy at this point and fork out later. Being a society most of us tend to make this happen for many factors. This includes trips, furniture, in addition to items we’d like. However, it is advisable to separate one’s wants from the needs. As long as you’re working to raise your credit ranking score actually you need some trade-offs. For example you may shop online to economize or you can check out second hand outlets instead of high priced department stores intended for clothing.

Leave a Reply

Your email address will not be published. Required fields are marked *