1

I need a code to help me print a square of stars with an Integer input (in this case the number is 5) the square must be empty on the inside.

for example:

Looking for this output

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

what I get

* * * * 
*       
*      
*       
* * * * 

I am missing my right side of the square.

MY code:

    System.out.print("Please enter a number: ");
    side = input.nextInt();
    for (int i = 0; i < side - 1; i++) {
        System.out.print("* ");

    }
    for (int i = 0; i < side; i++) {
        System.out.println("*");
    }
        
    for (int i = 0; i < side; i++) {
        System.out.print("* ");

    }

}

input

5

output

* * * * 
*       
*      
*       
* * * * 
vs97
  • 5,765
  • 3
  • 28
  • 41

3 Answers3

1

You can do this with a nested for loop.

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();
}

Print a * if it is either first row/column or last row/column; otherwise print two spaces.

Thiyagu
  • 17,362
  • 5
  • 42
  • 79
  • This will leave you with surplus space at the right edge of the square, which may not be what the OP (or his teacher) consider acceptable. (Though I've just spotted that the OP's solution will also leave surplus spaces at the right edge.) And this could be made more efficient, as described in my comment to Thomas M's answer. – Bobulous Oct 02 '20 at 18:48
1

It can be done using a single For Loop with a Complexity of O(n)

public static void main(String[] args) {
    System.out.print("Please Enter a Number ");
    Scanner scanner = new Scanner(System.in);
    int number = scanner.nextInt();
    if(number >= 3) {
        String endPattern = new String(new char[number]).replace("\0", " *");
        String midPattern = String.format("%-"+(number-2)*2+"s  %s", " *"," *");
        
        for(int i=1; i<= number; i++) {
            if(i==1 || i==number) {             
                System.out.print(endPattern);
            }
            else {
                System.out.print(midPattern);
            }
            System.out.println();
        }
    }
}

Output (for input 3)

Please Enter a Number 3
 * * *
 *   *
 * * *

output (for input 7)

Please Enter a Number 7
 * * * * * * *
 *           *
 *           *
 *           *
 *           *
 *           *
 * * * * * * *
Amit Kumar Lal
  • 5,537
  • 3
  • 19
  • 37
  • 1
    Yes, more efficient than nested for loops. (But don't forget that the `replace` and `format` methods both need to iterate characters to work, so this is more like consecutive for loops rather than just one. Still, this is unavoidable, and it's better than nesting loops, and then generating the same value over and over again.) – Bobulous Oct 03 '20 at 21:23
0

You need to add an extra star at the end of the middle for loop. This can be done by nested a second for loop of spaces followed by printing the star.

for (int i = 0; i < side; i++) {
    System.out.print("*");
    for (int j = 0; j < side; j++) {
        System.out.print("  ");
    }
    System.out.println("*");
}
Thomas M
  • 192
  • 1
  • 1
  • 10
  • 2
    Think about how you could make this more efficient. The inner loop will produce exactly the same sequence of characters each time, and in fact every line generated by the outer loop will be exactly the same sequence of characters each time. So why not use the inner loop just once (moving it before the outer loop) and then store the result in a temporary variable which you can use in the outer loop? That way you iterate only `side + side` times, instead of `side * side` times. – Bobulous Oct 02 '20 at 18:43
  • @Bobulous Hmm.. good idea! I wasn't thinking much about efficiency in this problem. Makes sense. Thanks! – Thomas M Oct 02 '20 at 18:44
  • 1
    Also note that you're not adding enough space to fill the air gap. Notice that the result shown by the OP has spaces between every star. So adding just `side` spaces inbetween the left and right edges won't give the correct appearance. – Bobulous Oct 02 '20 at 18:45
  • @Bobulous Yeah, that was excactly my problem. thanks for the advice. I will try and see if I can find the wanted result. –  Oct 02 '20 at 18:49
  • @Bobulous I have posted an answer of (O)n complexity. – Amit Kumar Lal Oct 02 '20 at 23:13