Java Program to Print Hollow Square Star Pattern

Write a Java Program to print the hollow square star pattern using for loop. This Java example allows entering square sides and uses nested for loop to iterate the sides. There is an if-else condition to check whether it is the first row, first column, last row, or last column. If it is true, print star; otherwise, print empty space.

package ShapePrograms;

import java.util.Scanner;

public class HollowSquare1 {
	private static Scanner sc;
	
	public static void main(String[] args) {
		sc = new Scanner(System.in);
		
		System.out.print("Enter Hollow Square Side = ");
		int side = sc.nextInt();
		
		System.out.println("Printing Hollow Square Star Pattern");
		
		for (int i = 0; i < side; i++ ) 
		{
			for (int j = 0 ; j < side; j++ ) 
			{
				if (i == 0 || i == side - 1 || j == 0 || j == side - 1) 
				{
					System.out.print("*");
				}
				else {
					System.out.print(" ");
				}
			}
			System.out.println();
		}
	}
}
Java Program to Print Hollow Square Star Pattern 1

In this Java program to display the hollow square pattern of stars, we replaced the for loop with nested while loops.

package ShapePrograms;

import java.util.Scanner;

public class HollowSquare2 {
	private static Scanner sc;
	
	public static void main(String[] args) {
		sc = new Scanner(System.in);
		
		System.out.print("Enter Hollow Square Side = ");
		int side = sc.nextInt();
		
		System.out.println("Printing Hollow Square Star Pattern");
		int j, i = 0; 
		while (i < side ) 
		{
			j = 0 ;
			while ( j < side ) 
			{
				if (i == 0 || i == side - 1 || j == 0 || j == side - 1) {
					System.out.print("*");
				}
				else {
					System.out.print(" ");
				}
				j++;
			}
			System.out.println();
			i++;
		}
	}
}
Enter Hollow Square Side = 12
Printing Hollow Square Star Pattern
************
*          *
*          *
*          *
*          *
*          *
*          *
*          *
*          *
*          *
*          *
************

Java Program to Print Hollow Square Star Pattern using do while loop

package ShapePrograms;

import java.util.Scanner;

public class HollowSquare3 {
	private static Scanner sc;
	
	public static void main(String[] args) {
		sc = new Scanner(System.in);
		
		System.out.print("Enter Hollow Square Side = ");
		int side = sc.nextInt();
		
		System.out.println("Printing Hollow Square Star Pattern");
		int j, i = 0; 
		do
		{
			j = 0 ;
			do
			{
				if (i == 0 || i == side - 1 || j == 0 || j == side - 1) {
					System.out.print("*");
				}
				else {
					System.out.print(" ");
				}
			} while (++j < side ) ;
			System.out.println();
		} while (++i < side ) ;
	}
}
Enter Hollow Square Side = 15
Printing Hollow Square Star Pattern
***************
*             *
*             *
*             *
*             *
*             *
*             *
*             *
*             *
*             *
*             *
*             *
*             *
*             *
***************

This Java code to print hollow square patterns allows entering their own symbol. Furthermore, we also created a HollowSquarePattern function that prints the given symbol in a hollow square pattern.

package ShapePrograms;

import java.util.Scanner;

public class HollowSquare4 {
	private static Scanner sc;
	
	public static void main(String[] args) {
		sc = new Scanner(System.in);
		
		System.out.print("Enter Hollow Square Side = ");
		int side = sc.nextInt();
		
		System.out.print("Enter Character for Hollow Square Pattern = ");
		char ch = sc.next().charAt(0);
		
		System.out.println("Printing Hollow Square Pattern");
		HollowSquarePattern(side, ch);
	}
	
	public static void HollowSquarePattern(int side, char ch) {
		for (int i = 0; i < side; i++ ) 
		{
			for (int j = 0 ; j < side; j++ ) 
			{
				if (i == 0 || i == side - 1 || j == 0 || j == side - 1) {
					System.out.print(ch);
				}
				else {
					System.out.print(" ");
				}
			}
			System.out.println();
		}
	}
}
Enter Hollow Square Side = 18
Enter Character for Hollow Square Pattern = #
Printing Hollow Square Pattern
##################
#                #
#                #
#                #
#                #
#                #
#                #
#                #
#                #
#                #
#                #
#                #
#                #
#                #
#                #
#                #
#                #
##################