Question 7: The output has an error only in the second column

package PatternsQuestions;

import java.util.Scanner;

public class ques7 {

public static void main(String[] args) {
	Scanner scn = new Scanner(System.in);
	int n = scn.nextInt();

	int nst=1;
	int nsp=0;
	int row = 1;
	
	while(row<=n){

// prinitng the first star
for(int cst=1; cst<= nst; cst++) {
System.out.print("*");
}

//setting if statement for space
int csp;
if(row==1 || row==n) {
csp=n+1;
} else {
csp=1;
}

		for(; csp<=nsp; csp++) {
			System.out.print(" ");
		}
		
		int cst2;
		if(row==1 || row==n) {
			cst2=n-2;
		} else {
			cst2=0;
		}
		
		for(int cst3 =1; cst3<=cst2; cst3++) {
			System.out.print("*");
		}
		
			
			for(int cst=1; cst<= nst; cst++) {
				   System.out.print("*");
				}
		
			System.out.println();
			if(row==1 || row==n) {
				nsp=0;
			} else {
				nsp=n-2;
			}
			row++;
			
	}

	scn.close();

}

}

I am getting an output like this for n = 5;


**


@sinuscoupon0s_4659768f9d9b587e Your code is quite complicated. To make it simple use this approach :
i)When you are in While loop put a if condition when row == 1 || row == n if this condition is true then only print n = 5 stars;
iii)for all other lines just print a single star give n-2 spaces then print a single star again.
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.