how can i create this pattern in a java loop with the use of input. for example input is x = 5 y = 6. this will be the output.

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

I've tried different code but it gets me to full block of asterisk i just want a rectangle that is blank inside.

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

please help. thank you so much.

Hello,

I really like prinring patterns, so I was attracted to your question. Here is my solution. It's probably not quite what you are looking for but here it is:

package test;

public class PrintPattern {

    public String printFullLine(final int thisMany) {
        String line = "";
        for (int i = 0; i < thisMany; i++) {
            line += ("*");
        }
        return line;
    }

    public String printFirstAndLast(final int thisMany) {
        String line = "";
        for (int i = 0; i < thisMany; i++) {
            if (i == 0 || i == thisMany-1) {
                line +=("*");
            } else {
                line += (" ");
            }
        }
        return line;
    }

    public static void main(String [] args) {

        PrintPattern pp = new PrintPattern();
        for (int i = 0; i < 6 ; i++){
            if (i == 0 || i==5){
                System.out.println(pp.printFullLine(6));
            } else {
                System.out.println(pp.printFirstAndLast(6));
            }
        }
    }
}

Hope this helps.

There is a simple logic to solve this problem,whenever i is zero or max,j is zero or max print "*" else print space " ".
This is a small code snippet.

        for (int i = 0; i < 6 ; i++){
            for(int j=0;j<6;j++){
                if(i==0||j==0||i==5||j==5){
                    System.out.print("*");
                }else{
                    System.out.print(" ");
                }

            }
            System.out.println();
  }

Thank you so much I've tried all your replies and it all worked. thank you so much.

I have a little problem when i tried to use the input of the user and tried other number other than 5 and 6 the result fail. how to fix this? ive tried this code.

    int n,m;

    System.out.print("Enter number n:");
    n = input.nextInt();
    System.out.print("Enter number m:");
    m = input.nextInt();

    for (int i = 0; i < n ; i++){
        for(int j=0;j<n;j++){
            if(i==0||j==0||i==m||j==m){
                System.out.print("*");
            }else{
                System.out.print(" ");
            }
        }
        System.out.println();
    }

Edited 3 Years Ago by eijnar

I've tried what you said but it give me this output.

Enter number n:3
Enter number m:4
****
*
*

Your for loop go from 0 to m-1 and 0 to n-1, so they never reach m or n
It owuld be clearer if you changed the loops to go from 1 to m and 1 to n

yo would also learn more if you don't go purely on the above snippets, but try to implement it yourself.
just looking at what is being spoonfed doesn't learn you anything,
step 1: analyse the problem
step 2: understand the problem
step 3: write the code
step 4: test
step 5: if need be, debug and return to step 4

look for suggestions rather than implemented code you can copy paste.

I actually don't want to just write code for you,but will like to guide you how exactly to do that.
1)

for (int i = 0; i < n ; i++){

Your first loop is for iterating rows.

2)

for(int j=0;j<m;j++){//here it should be m not n,you have taken both n,that's the wrong thing,each row

This loop is for iterating columns of the ith row.

3)Now only logic that you have to think is when to print "*" and when to print space.

So,the logic will be whenever your row is minimum or max value of row ,print "*" and same for column,else print " " space.
Hopefully it is enough to code.

Edited 3 Years Ago by IIM

This article has been dead for over six months. Start a new discussion instead.