# star array

• 11-23-2008, 08:17 AM
nanna
star array
Hi,
Iam writting program with array. This program should count the numbers of star equal to each element in the array.
the output should be as:
0|****(4)
1|**(2)
3|*********(9)
4|************(12)
5|********(8)
6|***(3)
7|*******(7)

Code:

```/** This program will calculate the numbers of     stars equal to each element of the array.  */ public class StarNumber {   public static void main(String args[]) {       int[] numbers={4,2,9,12,8,3,7};           for (int index=0; index<numbers.length; index++)               System.out.println((index) + "|*" +numbers[index]);   } }```
My Output:
0|*4
1|*2
2|*9
3|*12
4|*8
5|*3
6|*7
• 11-23-2008, 08:36 AM
Fubarable
We wish you well with your program.

BTW, did you have a question?
• 11-23-2008, 08:39 AM
nanna
Hi,
ofcourse I have a question ??
I want to know what is missing in my program to get the required output ?
• 11-23-2008, 08:41 AM
carlodelmundo
Nanna,

I had this problem a long time back. Try using another for loop inside your existing for loop.

Remember what you're trying to achieve. The first for loop wants to traverse through the total number of array elements (your initial for loop is a great start!). Now, to print the asterisks (*), you need to use a second for loop inside your existing loop.

What's the purpose of your second for loop? We want to create the asterisks and the number of asterisks depends on the value at a certain index of your array. Iterate that many times. For example, at index [0], we have a value of 4. Iterate from 0 to 4, and at each iteration print out an asterisk.
• 11-23-2008, 08:58 AM
Fubarable
Quote:

ofcourse I have a question ??
Hopefully Carlo has answered your question (as hopefully I have answered his), but in the future, you will find it much easier to get helpful replies if you spell out your question in as specific a way as possible. Since we're volunteers here, you want to make it as easy as possible for others to help you, right?