1

If I want to print the pattern below, and I have an Array:

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

This is what I have so far:

public static void main(String[] args) {
    int[] p = {7,5,3,1,3,5,7};

    for (int i=0; i <= 7; i++) {
        System.out.print("\n");
        for (int k =i; k <= 7; k++) {
             k = p[i];  
             // I'm trying to find a way to print "*"s with array element value
             for(int j=i; j <= 7; j++) {
                 System.out.print("*");
             }
        }
    }
}

I definitely went wrong somewhere, and I apologize for my ignorant. I'm just trying to learn. Thanks!

5
  • 1
    First of all you should use ' < ' in for loop instead of ' <= ' because you have 7 elements from 0 to 6 not eight from 0 to 7 :) Commented May 29, 2017 at 5:30
  • personally, I won't store the values in an array. Think about it this way, if stars = 7 then spaces = 0. when stars are reduced by 2, spaces are increased by one. until stars are 1, then reverse. Commented May 29, 2017 at 5:32
  • @MichaelMarkidis Are you coming from a C background? In Java, typically we place the opening brackets on the same line as the construct. Commented May 29, 2017 at 6:00
  • @TimBiegeleisen No, I personally never liked that style. Commented May 29, 2017 at 6:05
  • You are out of bounds on your Array. Inner loop. Zero based indexing. Commented May 31, 2017 at 19:17

5 Answers 5

1

Try this out:

int [] p = { 7, 5, 3, 1, 3, 5, 7 };

// as mentioned in the comment, you want < here and not <=
for (int i = 0; i < p.length; i++)
{
    // total number of spaces needed
    int numSpaces = p[0] - p[i];

    // this array will hold the '*'s
    char [] arr = new char[p[i]];

    // half the spaces for each side
    char [] spaces = new char [numSpaces / 2];

    // fill the arrays
    Arrays.fill(arr, '*');
    Arrays.fill(spaces, ' ');

    // build the string
    StringBuilder sb = new StringBuilder();
    sb.append(spaces);
    sb.append(arr);
    sb.append(spaces);

    System.out.println(sb);
}

Output:

*******
 ***** 
  ***  
   *   
  ***  
 ***** 
*******
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you! this is really easy to understand.
1

Consider the following solution which builds your output string by making one pass through all distinct star patterns. Appreciate that because your pattern is symmetric, we only need to generate one half of it, as the other half is simply a mirror image.

String top = "";
String bottom = "";
int maxStars = 7;
for (int i=0; i < maxStars; i=i+2) {
    StringBuilder line = new StringBuilder();
    for (int j=0; j < i/2; ++j)          line.append(" ");  // left spaces
    for (int j=0; j < maxStars - i; ++j) line.append("*");  // stars
    for (int j=0; j < i/2; ++j)          line.append(" ");  // right spaces
    top += line + "\n";
    // this if check prevents us from adding the single star row twice
    if (maxStars - i > 1) {
        bottom = line + "\n" + bottom;
    }
}
String pattern = top + bottom;
System.out.println(pattern);

Output:

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

Demo here:

Rextester

1 Comment

genius. i like it.
1
public static void main(String[] args) {

    int[] p = { 7, 5, 3, 1, 3, 5, 7 };

    for (int i = 0; i < p.length; i++) {
        for(int k=p[0]-p[i], m = 0;m<k;m++)    System.out.print(" ");
        for( int j = 0; j<p[i]; j++)           System.out.print(" *");
        System.out.println();
    }}

OUTPUT

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

2 Comments

Your outer for loop had the wrong bounds, but I corrected it +1
I am a beginner so I really love this solution. it is easy to understand for me. Thanks!
0

Use string-repeat in that case:

String.join('', Collections.nCopies(p[i], '*'));

or

StringUtils.repeat('*', p[i]);

Comments

0

Try This Logic Work Fine With Your Requirements

public class Test {

public static void main(String[] args) {

int[] p = {7,5,3,1,3,5,7};

    for (int i = 0; i < p.length; i++) {

        int temp = p[i];

        if (i <= p.length/2) {

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

                System.out.print(" ");

            }

        } else {

            for (int whiteSpace = p.length - i - 1; whiteSpace > 0; whiteSpace--) {

                System.out.print(" ");

            }


        }

        for (int j = 0; j < temp; j++) {

            System.out.print("*");

        }

        System.out.println("");



    }

}

**Output : **

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

1 Comment

I like how you declare temp. It's really handy, and I'll definitely use it in the future.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.