1. Member
Join Date
Oct 2008
Posts
19
Rep Power
0

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)

Java 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

2. We wish you well with your program.

BTW, did you have a question?

3. Member
Join Date
Oct 2008
Posts
19
Rep Power
0
Hi,
ofcourse I have a question ??
I want to know what is missing in my program to get the required output ?

4. Member
Join Date
Aug 2008
Posts
31
Rep Power
0
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.

5. 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?

Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•