Hey guys and gals I need alittle help. i have an assignment that needs to print a certian number of stairs according to a user input.

Example program run (user input is in bold):
Please enter the number of rows: 4
Please enter the number of stars in row 1: 8
Please enter the number of stars in row 2: 2
Please enter the number of stars in row 3: 4
Please enter the number of stars in row 4: 1
********
**
****
*

this is my code so far :

public class Main {
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {

        int rows;
        Scanner keyboard = new Scanner(System.in);
        //print splash screen
        System.out.println("Welcome to Star Shapes ...");
        System.out.println("\t... a program which lets you draw star shapes\n" +
                "\t    First enter the number of rows (1-10) then,\n" +
                "\t    Enter the number of stars for each row (1-10)\n" +
                "\t    Okay lets start:\n");
        System.out.print("How many rows would you like ?  ");
        rows = keyboard.nextInt();

        if (rows > 10) {
            System.out.println("you can only have a max of 10 rows try again");
        } else if (rows < 1) {
            System.out.println("you have to have at least 1 row try again");
        } else if (rows >= 1) {
            if (rows <= 10) {
                
                    System.out.print("Please enter the number of stars in row 1..");
                    int row1 = keyboard.nextInt();
                    System.out.print("Please enter the number of stars in row 2..");
                    int row2 = keyboard.nextInt();
                    System.out.print("Please enter the number of stars in row 3..");
                    int row3 = keyboard.nextInt();
                    System.out.print("Please enter the number of stars in row 4..");
                    int row4 = keyboard.nextInt();
                    System.out.print("Please enter the number of stars in row 5..");
                    int row5 = keyboard.nextInt();
                    System.out.print("Please enter the number of stars in row 6..");
                    int row6 = keyboard.nextInt();
                    System.out.print("Please enter the number of stars in row 7..");
                    int row7 = keyboard.nextInt();
                    System.out.print("Please enter the number of stars in row 8..");
                    int row8 = keyboard.nextInt();
                    System.out.print("Please enter the number of stars in row 9..");
                    int row9 = keyboard.nextInt();
                    System.out.print("Please enter the number of stars in row 10..");
                    int row10 = keyboard.nextInt();
               



                    int[] shapeArray;              // declares an array of integers

                    shapeArray = new int[10];      // allocates memory for 10 integers

                    shapeArray[0] = row1; // initialize first element
                    shapeArray[1] = row2; // initialize second element
                    shapeArray[2] = row3; // etc.
                    shapeArray[3] = row4;
                    shapeArray[4] = row5;
                    shapeArray[5] = row6;
                    shapeArray[6] = row7;
                    shapeArray[7] = row8;
                    shapeArray[8] = row9;
                    shapeArray[9] = row10;


                    for (int i = 0; i < rows; i++) {
                        for (int countt = 0; countt < shapeArray[i]; countt++) {
                            String printString = "*";
                            System.out.print(printString);
                        }
                        System.out.println((shapeArray[i]));
                    }
                }
            }

        }
    }

and here is my output:

Welcome to Star Shapes ...
        ... a program which lets you draw star shapes
            First enter the number of rows (1-10) then,
            Enter the number of stars for each row (1-10)
            Okay lets start:

How many rows would you like ?  5
Please enter the number of stars in row 1..3
Please enter the number of stars in row 2..7
Please enter the number of stars in row 3..2
Please enter the number of stars in row 4..9
Please enter the number of stars in row 5..4
Please enter the number of stars in row 6..1
Please enter the number of stars in row 7..1
Please enter the number of stars in row 8..1
Please enter the number of stars in row 9..1
Please enter the number of stars in row 10..1
***3
*******7
**2
*********9
****4

everything works great expect my print statement continue after the number of rows are declared. in this example its 5 so there is no need for 6 7 8 ect.. i would love it to say after the fifth row have a statement that says "hit return to draw your stars" and then after that print the stars.

A few more minds on this would help a lot.
Thanks !!

You want to put your prompt ("Enter the number of stars in row N") in a for loop, with rows as the upper bound.

i thought of this i did something like this

for (int j=1; j<= rows;j++){
                    System.out.print("Please enter the number of stars in row" + j + "..");
                    int row1 = keyboard.nextInt();
                    int row2 = keyboard.nextInt();
                    int row3 = keyboard.nextInt();
                    int row4 = keyboard.nextInt();
                    int row5 = keyboard.nextInt();
                    int row6 = keyboard.nextInt();
                    int row7 = keyboard.nextInt();
                    int row8 = keyboard.nextInt();
                    int row9 = keyboard.nextInt();          
                    int row10 = keyboard.nextInt();

and this is the output:

run:
Welcome to Star Shapes ...
        ... a program which lets you draw star shapes
            First enter the number of rows (1-10) then,
            Enter the number of stars for each row (1-10)
            Okay lets start:

How many rows would you like ?  5
Please enter the number of stars in row1..2
1
1
1
1
1
1
1
1
1
**2
*1
*1
*1
*1
Please enter the number of stars in row2..1

so there is still something im missing :/ and i think it has to do with the array....

Yes. You should use an array. :)

For loops and arrays are a well-matched pair. If you have N elements to deal with, put them in an array, elements[N], and then deal with them each in turn by
looping from 0..N-1 and doing the right thing to elements, where i is your index variable.
In this case, the "right thing" is to prompt for a number and get the input.

You have an array, it's called shapeArray. First of all, you're getting a number from the user, which is the number of rows they want to use. That's the size of shapeRow - if they want to enter 6 rows, there's no need for shapeRow to be 10 elements, and if they want 13, 10's not enough. So instead of

int[] shapeArray;              
 shapeArray = new int[10];

why not let shapeArray be

shapeArray = new int[rows];

Now, you've got an array of ints - why declare ten individual ints, when all you're doing is putting those values into shapeArray? Why not just put them straight into shapeArray?
You could do that by writing individual lines of code, but that's silly.
You obviously know how to write a for loop, because you've got one. So write another,
only in this one you're going to loop on i from 0 to rows-1, and for each i, you're going to get a number from the user and put it in shapeArray - you've done that, it looks like this:

Okay, now you can print out the lines, and you've already got that part. It looks like this:

System.out.print("Please enter the number of stars in row 1..");
                    int row1 = keyboard.nextInt();

but instead of row1, you want shapeArray

Better?

Much better :) thank you tons its always little things that trip me up. but now im getting a error :/

rewritten code which is much cleaner :)

int[] shapeArray;
                shapeArray = new int[rows];

                for (int j = 1; j <= rows; j++) {
                    System.out.print("Please enter the number of stars in row " + j + "..");
                    shapeArray[j] = keyboard.nextInt(); // error is being thrown here
                }

                for (int i = 0; i < rows; i++) {
                    for (int countt = 0; countt < shapeArray[i]; countt++) {
                        String printString = "*";
                        System.out.print(printString);
                    }
                    System.out.println((shapeArray[i]));
                }
            }

and here is the out put

Welcome to Star Shapes ...
        ... a program which lets you draw star shapes
            First enter the number of rows (1-10) then,
            Enter the number of stars for each row (1-10)
            Okay lets start:

How many rows would you like ?  5
Please enter the number of stars in row 1..2
Please enter the number of stars in row 2..3
Please enter the number of stars in row 3..4
Please enter the number of stars in row 4..1
Please enter the number of stars in row 5..2
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 5
        at hw1stars.Main.main(Main.java:45)
Java Result: 1
BUILD SUCCESSFUL (total time: 25 seconds)

jon.kiparsky you have been a great help thanks you :)

Ok so i fixed the error by adding a try and catch statement and it works great !!!

int[] shapeArray;            // declare an in array
                shapeArray = new int[rows];  // new array with size rows
                try {

                    for (int j = 1; j <= rows; j++) {
                        System.out.print("Please enter the number of stars in row " + j + "..");
                        shapeArray[j - 1] = keyboard.nextInt(); // filling the array
                    }
                } catch (ArrayIndexOutOfBoundsException e) {  // catches the execption
                    e.printStackTrace();
                }


                for (int i = 0; i < rows; i++) {
                    for (int count = 0; count < shapeArray[i]; count++) {
                        String printString = "*";
                        System.out.print(printString);
                    }
                    System.out.println(shapeArray[i]);
                }
            }
        }

    }
}

one last minor tweak i need to do. how can i get the numbers off of the end of the line that has stars. for example the 3 ,4 ,5 in my output

:
Welcome to Star Shapes ...
        ... a program which lets you draw star shapes
            First enter the number of rows (1-10) then,
            Enter the number of stars for each row (1-10)
            Okay lets start:

How many rows would you like ?  3
Please enter the number of stars in row 1..4
Please enter the number of stars in row 2..3
Please enter the number of stars in row 3..5
****4 // these numbers 
***3 //
*****5 //

i know it has to do with my nested for loop but i cant seem to get it :/

Ok so i fixed the error by adding a try and catch statement and it works great !!!

int[] shapeArray;            // declare an in array
                shapeArray = new int[rows];  // new array with size rows
                try {

                    for (int j = 1; j <= rows; j++) {
                        System.out.print("Please enter the number of stars in row " + j + "..");
                        shapeArray[j - 1] = keyboard.nextInt(); // filling the array
                    }
                } catch (ArrayIndexOutOfBoundsException e) {  // catches the execption
                    e.printStackTrace();
                }


                for (int i = 0; i < rows; i++) {
                    for (int count = 0; count < shapeArray[i]; count++) {
                        String printString = "*";
                        System.out.print(printString);
                    }
                    System.out.println(shapeArray[i]);
                }
            }
        }

    }
}

one last minor tweak i need to do. how can i get the numbers off of the end of the line that has stars. for example the 3 ,4 ,5 in my output

:
Welcome to Star Shapes ...
        ... a program which lets you draw star shapes
            First enter the number of rows (1-10) then,
            Enter the number of stars for each row (1-10)
            Okay lets start:

How many rows would you like ?  3
Please enter the number of stars in row 1..4
Please enter the number of stars in row 2..3
Please enter the number of stars in row 3..5
****4 // these numbers 
***3 //
*****5 //

i know it has to do with my nested for loop but i cant seem to get it :/

Actually it wasn't the try catch that you added. You should never catch that exception. It is bad coding. It means that you made a mistake when accessing arrays and you didn't use the correct index.

What you did is this:
shapeArray[j - 1]

At first the index j is 1. But the indexes of arrays start from 0. So when you do: j-1, and you loop from 1 to rows, you fixed your problem.

You could also done this:

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

  System.out.print("Please enter the number of stars in row " + (j+1) + "..");

  shapeArray[j] = keyboard.nextInt(); // filling the array
}

Also for the last question, the numbers are printed, because you tell them to. Look at your System.outs, what you print after the inner loop.
Also if you want an empty line, you can do this: System.out.println();

WOW !!!
Thanks so much that was it :) i get frustrated when its just minor bumps that hold me down...

Thanks again guys :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.