Hi,
I need to show stars equal to the each number of an array. I think I need another for statement. I`ve tried, but in vain. I do not know which variables should be included in this for statement ??
the output should be :
0|****(4)
1|**(2)
2|*********(9) and so forth.

/** 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

To do this problem you are going to want to use a nested for loop. The first loop you have is good. Inside of that loop you will want to include one that counts from 0 to the value of the array at that particular index value. Within the second loop you will do the printing of the '*'.

and in that second loop, you use the value in that position in the array as maxValue

you will want something like;

for (int index=0; index <numbers.length; index++){
//Statement printing the number ie. 0, 1, 2 ,3...etc
       for(int index1=0; index<numbers.length; index1++){
//statement printing thestars
   }
//statement toprint new line
}

you cannot put the printstatement comtaining the | and the \n in the inside for loop because it will print it everytime and you only want it before and after the stars respectively.

you will want something like;

for (int index=0; index <numbers.length; index++){
//Statement printing the number ie. 0, 1, 2 ,3...etc
       for(int index1=0; index<numbers.length; index1++){
//statement printing thestars
   }
//statement toprint new line
}

you cannot put the printstatement comtaining the | and the \n in the inside for loop because it will print it everytime and you only want it before and after the stars respectively.

Beside the fact that the second for loop is wrong, the others (stultuske, Fuze) have tried to help him out by giving him hints on how to solve it on his own.
It would be very easy for them to post the correct code, but nanna wouldn't have learned much that way.

Oh, sorry I wasnt aware you never made mistakes....

for (int index1=0; index1<numbers[index]; index1++){

I didnt post hardly any code, I posted 2 lines and a few comments. The rest need to be figured out. Sitting and starring at code for an hour waiting for it to come to you doesn't happen, sometimes you need to be pointed in the right direction and then you can get the answer from there, any coder knows that anyways.

So what is the solution ??????????
Do not be ambiguous , pls I need direct help

The solution was basically posted by poggie. Just have to look at the edit he made to the 2nd for loop.

Sitting and starring at code for an hour waiting for it to come to you doesn't happen, sometimes you need to be pointed in the right direction and then you can get the answer from there, any coder knows that anyways.

no it doesn't. but if you're trying to learn, it might help to stare and search for yourself. we handed the concept, you basically handed a complete meal ( you just forgot to give the napkins)

@Nana:
if it still doesn't work, show us how your code looks like now
if you haven't changed it yet, adapt it following the hints above, test that and see if that does you any good.

Hi,
This is my modified program, but the output is not the required one ???

/** 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++)
      
       
      for (int index1=0; index1<numbers[index]; index1++) {
      System.out.println((index) + "|* " + numbers[index]);
      
    } 
  }
}

my output:
0|* 4
0|* 4
0|* 4
1|* 2
2|* 9
2|* 9
2|* 9
2|* 9
2|* 9
2|* 9
2|* 9
2|* 9
3|* 12
3|* 12
3|* 12
3|* 12
3|* 12
3|* 12
3|* 12
3|* 12
3|* 12
3|* 12
3|* 12
4|* 8
4|* 8
4|* 8
4|* 8
4|* 8
4|* 8
4|* 8
5|* 3
5|* 3
6|* 7
6|* 7
6|* 7
6|* 7
6|* 7
6|* 7

The loop idea you have is right, just not put together properly

You need to nest the loops within one another.
The first loop you have there does absolutely nothing

 for (int index=0; index<numbers.length; index++){
     //print index value here

 for(int index1=0; index1<numbers[index]; index1++) {
     //print stars here
 }

}

loop through each index of the array{
loop from 0 to the number at the current index of the array - 1{
print the star
}
}

For the first loop, consider that you want to print the stars for each index of the array. One way to do this is to go through each index of the array. At each step of the way, look at the index you are on and print the same number of *'s. For the second loop, notice that I say star, not stars. Think about the relationship between the loop and the print statement for the star. I didn't really add anything to what anyone else said - just put it in a different format in case you were having trouble thinking about it. Good luck

:)

This is the modified program , but it repeats each integer in the array instead of printing astrisks equal to that integer??
The problem is in the variables of second for loop, but i could not fix it ??

/** 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);
              
      for (int count=0; count<numbers[index]; count++) {
       
      System.out.println("|* " + numbers[index]);
     
      } 
    } 
  }
}

my output:
0
|* 4
|* 4
|* 4
|* 4
1
|* 2
|* 2
2
|* 9
|* 9
|* 9
|* 9
|* 9
|* 9
|* 9
|* 9
|* 9
3
|* 12
|* 12
|* 12
|* 12
|* 12
|* 12
|* 12
|* 12
|* 12
|* 12
|* 12
|* 12
4
|* 8
|* 8
|* 8
|* 8
|* 8
|* 8
|* 8
|* 8
5
|* 3
|* 3
|* 3
6
|* 7
|* 7
|* 7
|* 7
|* 7
|* 7
|* 7

/** 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);
      String printString = "";
      for (int count=0; count<numbers[index]; count++) {
       
      printString += "*";
     
      } 
      System.out.println(printString);
    } 
  }
}

it is doing exactly what you program it to do.
test the above code, and check the differences in this code and yours, and the difference in the output.

normally I would not just give you the code, but since this sollution was suggested in the first answers already ...

Edit to code above, minor minor change but for the desired out put you want you will need to change the first System.out.println(index); line to System.out.print(index);

This way, you will keep everything on the same line and not have the index number above all the of the stars being printed for that index location.

Hi,
This is my final modified program. I`ve made some changes on the last posted code and it`s ok. finally I get the required output ????
Thanks

/** 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.print(index);
         String printString="";
              
      for (int count=0; count<numbers[index]; count++) {
        printString= "*";
       
      System.out.print(printString);
       
     }
      System.out.println((numbers[index]));
     
      
    } 
  }
}

output:
0****4
1**2
2*********9
3************12
4********8
5***3
6*******7

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.