Results 1 to 5 of 5

Thread: star array

  1. #1
    nanna is offline Member
    Join Date
    Oct 2008
    Posts
    19
    Rep Power
    0

    Default 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. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    We wish you well with your program.

    BTW, did you have a question?

  3. #3
    nanna is offline Member
    Join Date
    Oct 2008
    Posts
    19
    Rep Power
    0

    Default

    Hi,
    ofcourse I have a question ??
    I want to know what is missing in my program to get the required output ?

  4. #4
    carlodelmundo is offline Member
    Join Date
    Aug 2008
    Posts
    31
    Rep Power
    0

    Default

    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. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    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?

Similar Threads

  1. Star pattern help?
    By GeeKunMow in forum New To Java
    Replies: 13
    Last Post: 07-21-2011, 05:59 AM
  2. creating a Star
    By jhen in forum New To Java
    Replies: 2
    Last Post: 11-19-2008, 03:41 AM
  3. Replace number to the star
    By linux0kernel in forum New To Java
    Replies: 5
    Last Post: 10-15-2008, 09:39 AM
  4. help with an array.
    By f_the_cook in forum New To Java
    Replies: 7
    Last Post: 06-03-2008, 04:05 AM
  5. Array Reflection: Multi Array Reflection
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-23-2008, 08:08 PM

Posting Permissions

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