Hollow Square of 1s Pattern in Java

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:
1 1 1 1 1
1 1
1 1
1 1
1 1 1 1 1Complete Java Program
Print 1 on the border (first/last row or first/last column). Otherwise print spaces.
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
Choose the grid size
rows = 5 means we print a 5×5 square.
Outer loop controls rows
i = 1..rows iterates over each row of the square.
Inner loop controls columns
j = 1..rows prints each column position in the current row.
Border check prints 1s
If you are on the first/last row or first/last column, print "1 "; otherwise print spaces.
Hollow border square
Only border cells are filled; inner cells are blank. Total iterations are rows×rows, so time complexity is O(n²).
Variation — User Input Version
Read the square size at runtime using Scanner:
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
ifcondition - Create a hollow rectangle by using different row and column sizes
- Use
StringBuilderto 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
5everywhere instead of usingrows - Forgetting the newline after each row
- Changing spaces inside the square (it will affect how “hollow” looks)
- Closing
System.inwithScannerif you need console input later in the same JVM run
Key Takeaways
Use nested loops to traverse a rows×rows grid.
Border cells satisfy i==1, i==rows, j==1, or j==rows.
Everything else (inner cells) prints spaces to make the square hollow.
Time complexity is O(n²) because you visit every cell once.
❓ Frequently Asked Questions
1; the columns in between print spaces. That creates the hollow look.if condition and always print "1 " inside the inner loop.rows and cols. Loop i=1..rows and j=1..cols, and treat j==cols as the last column.Explore More Java Number Patterns!
Border patterns are a great next step after basic triangles and pyramids.
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.
12 people found this page helpful
