How do I print a hollow square pattern using loops in Java?

You can print a hollow square by combining nested for loops with an if statement. The loops visit every position in a square grid, while the condition prints stars only along its border. Positions inside the square contain spaces.

Source code

Save this example as HollowSquarePatternExample.java.

public class HollowSquarePatternExample {
    public static void main(String[] args) {
        int size = 5;

        for (int row = 0; row < size; row++) {
            for (int column = 0; column < size; column++) {
                if (row == 0 || row == size - 1
                        || column == 0 || column == size - 1) {
                    System.out.print("* ");
                } else {
                    System.out.print("  ");
                }
            }
            System.out.println();
        }
    }
}

Explanation

The variable size controls both the number of rows and the number of logical columns. The outer loop visits each row, and the inner loop visits each column in that row. Both counters start at 0 and stop before size, so the last index is size - 1.

A position belongs to the border when at least one of these conditions is true:

Condition Border
row == 0 Top
row == size - 1 Bottom
column == 0 Left
column == size - 1 Right

The || operator means logical OR. If any border condition is true, the program prints "* ". Otherwise, it prints " " to leave the interior empty.

Both strings occupy two character positions. Using only one space for an empty position would shift the right border to the left on the middle rows.

For size = 5, the first and last rows contain five stars each. The three middle rows contain a star at each end and three empty positions between them. The output contains 16 stars in total.

System.out.print() stays on the current line. After the inner loop finishes, System.out.println() starts the next row. Keep this line break inside the outer loop and outside the inner loop.

Run the example

With a JDK installed, open a terminal in the directory containing the source file and run:

javac HollowSquarePatternExample.java
java HollowSquarePatternExample

Output

* * * * *
*       *
*       *
*       *
* * * * *

Display the output in a monospaced font to keep the pattern aligned. Trailing spaces are omitted from the output shown here. The pattern has equal numbers of logical rows and columns; its apparent proportions depend on the font and character spacing.

Try it yourself

Change size to 3, then to 7. For a size of at least two, the number of border stars is 4 * (size - 1): the top and bottom rows contribute 2 * size stars, and the remaining rows contribute 2 * (size - 2) stars.

Also try size = 1 and size = 2. The first prints a single star; the second prints four stars. Neither has an empty interior. A zero or negative size produces no output because the outer loop never executes.

To review how nested loops print every position, see How do I print a square pattern using loops in Java?.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.