Is this approach correct? I got the required output but is there any other way to do this with lesser amount of code?

package PatternsQuestions;

import java.util.Scanner;

public class ques7part2 {

public static void main(String[] args) {
	Scanner scn = new Scanner(System.in);
	int n = scn.nextInt();
	int nst = 1;
	int nsp = 1;
	
	for(int row = 1; row<=n; row++) {
		
		
		//working
		
		// if conditions for the first and nth row
		if(row==1 || row==n) {
		for(int count=1; count<=n-1; count++) {
		for (int cst=1; cst<=nst; cst++) {
			System.out.print("*");
		}
		
		for(int csp=1; csp<=nsp; csp++) {
			System.out.print(" ");
		}
		}
		} else { // condition for rest of the rows
			System.out.print("*");
			for(int spc = 1; spc<=2*n-3; spc++) {
				System.out.print(" ");
			}
			
		}
		System.out.print("*");
		
		
		
		//preparation
		System.out.println();
		
		
		
	}
	
	scn.close();
}

}

@sinuscoupon0s_4659768f9d9b587e For simple approach code will look like this :

import java.util.Scanner;

public class Main {

public static void main(String[] args) {
   Scanner scn = new Scanner(System.in);
   int n = scn.nextInt();
   int row = 1;
   while(row<=n){
       if(row == 1 || row == n){
           for(int i = 0; i < n; i++){
               System.out.print("* ");
           }
       }else{
           //single star 
           System.out.print("* ");
           //n-2 spaces
           for(int i = 0; i < n-2; i++){
               System.out.print("  ");
           }//single star 
           System.out.print("* ");
       }
   		row++;
           System.out.println();
   		
   }

   scn.close();

}
}

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.