Hollow Square of 1s Pattern in Java

Beginner
⏱️ 5 min read
📚 Updated: Aug 2025
🎯 2 Code Examples
Nested Loops

What You’ll Learn

How to print a hollow square pattern in Java where the border is made of 1s and the inside is blank.

This is a classic nested-loop exercise that teaches you how to detect boundary cells with simple conditions.

⭐ Pattern Output

For rows = 5, the pattern looks like this:

Output
1 1 1 1 1
1       1
1       1
1       1
1 1 1 1 1
1

Complete Java Program

Print 1 on the border (first/last row or first/last column). Otherwise print spaces.

Java
public class Main {
    public static void main(String[] args) {
        int rows = 5;

        for (int i = 1; i <= rows; i++) {
            for (int j = 1; j <= rows; j++) {
                if (i == 1 || i == rows || j == 1 || j == rows) {
                    System.out.print("1 ");
                } else {
                    System.out.print("  ");
                }
            }
            System.out.println();
        }
    }
}

🧠 How It Works

1

Choose the grid size

rows = 5 means we print a 5×5 square.

Setup
2

Outer loop controls rows

i = 1..rows iterates over each row of the square.

Row control
3

Inner loop controls columns

j = 1..rows prints each column position in the current row.

Column control
4

Border check prints 1s

If you are on the first/last row or first/last column, print "1 "; otherwise print spaces.

Condition
=

Hollow border square

Only border cells are filled; inner cells are blank. Total iterations are rows×rows, so time complexity is O(n²).

2

Variation — User Input Version

Read the square size at runtime using Scanner:

Java
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter the number of rows: ");
        int rows = sc.nextInt();

        for (int i = 1; i <= rows; i++) {
            for (int j = 1; j <= rows; j++) {
                if (i == 1 || i == rows || j == 1 || j == rows) {
                    System.out.print("1 ");
                } else {
                    System.out.print("  ");
                }
            }
            System.out.println();
        }

        sc.close();
    }
}

💡 Tips for Enhancement

Try These

  • Print a different border digit (like 0) or a character (like *)
  • Print a filled square by removing the if condition
  • Create a hollow rectangle by using different row and column sizes
  • Use StringBuilder to build each line if you want more control over spacing
  • Add a second border (a frame) by checking i==2, i==rows-1, etc.

Avoid

  • Hard-coding 5 everywhere instead of using rows
  • Forgetting the newline after each row
  • Changing spaces inside the square (it will affect how “hollow” looks)
  • Closing System.in with Scanner if you need console input later in the same JVM run

Key Takeaways

1

Use nested loops to traverse a rows×rows grid.

2

Border cells satisfy i==1, i==rows, j==1, or j==rows.

3

Everything else (inner cells) prints spaces to make the square hollow.

4

Time complexity is O(n²) because you visit every cell once.

❓ Frequently Asked Questions

Only the first and last columns print 1; the columns in between print spaces. That creates the hollow look.
Remove the if condition and always print "1 " inside the inner loop.
Yes. Use two variables: rows and cols. Loop i=1..rows and j=1..cols, and treat j==cols as the last column.
O(n²) for an n×n square due to two nested loops.

Explore More Java Number Patterns!

Border patterns are a great next step after basic triangles and pyramids.

All Number Patterns →
Did you know?

Hollow shapes are built with the same idea: print the value on the boundary, and print spaces everywhere else. The same condition works for hollow rectangles, frames, and even hollow diamonds.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

12 people found this page helpful