
Suzanne O. answered 10/12/19
International Experience and Multiple State Certifications
You have a good start. For the problem, I would use nested for loops: one loop reads the array box by box and prints the array index and the value in that box (just like the example); the second loop prints the stars according to the value in the box.
In the snippet of code above, you would want to move your println() outside of the print loop. That way it will print the stars all on the same line, and move to the next line before printing anything else or ending the program.
If you wanted to jazz up the program, you would let the user input the data and print a custom histogram.
Good luck!
Ashley P.
class arrays { public static void main(String[] args) { int[] array1 = {5, 11, 4, 1, 7, 10, 0, 9, 12}; int i = 0; for (int k = 0; k <= 9; k++) { for (int x = array1[i]; i <= 9; i++) { System.out.println(array1[i]); // for (int j = array1[i]; i <= 9; i++) { System.out.print("*"); } System.out.println(); } } } }10/14/19
Ashley P.
Unfortunately, the code doesn't follow the formatting here. Sorry for the inconveniences caused!10/14/19
Ashley P.
I was able to print the first two columns as it is in the question, but not the stars. class Arrays2 { public static void main(String[] args) { int[] array1 = {5, 11, 4, 1, 7, 10, 0, 9, 12}; for (int j = 0; j <= 8; j++) { System.out.println(j+" "+array1[j]); } for (int i = 0; i <= 8; i++) { for (int x = 0; x < array1[i]; x++) { System.out.print("*"); } System.out.println(); } } } Output : 0 5 1 11 2 4 3 1 4 7 5 10 6 0 7 9 8 12 ***** *********** **** * ******* ********** ********* ************ Could you think of a way to print the stars next to each value? Thanks!10/14/19

Suzanne O.
I see a few things. Y ou are using system.out.println(j+" "+array1[j]);, but it should be a .print (stays on the same line), not a .println (has a line return in it). That fix alone should clear things up. but I would suggest for a bit more efficiency in the program that you move the star-printing for loop inside you first loop, so that you have a double loop and not a single then a double loop. Code would look similar to this: class Arrays2 { public static void main(String[] args) { int[] array1 = {5, 11, 4, 1, 7, 10, 0, 9, 12}; for (int j = 0; j <= 8; j++) { system.out.print(j+" "+array1[j])+" "; //.print, not .println for (int x = 1; x <= array1[j] x++) { system.out.print("*"); } system.out.println(); } }10/14/19
Ashley P.
Thanks a lot for the response. But still I don't get how to get the stars printed according to the value. Below is my next attempt. Would you mind clarifying this please? class arrays { public static void main(String[] args) { int[] array1 = {5, 11, 4, 1, 7, 10, 0, 9, 12}; int i = 0; for (int k = 0; k <= 9; k++) { for (int x = array1[i]; i <= 9; i++) { System.out.println(array1[i]); // for (int j = array1[i]; i <= 9; i++) { System.out.print("*"); } System.out.println(); } } } }10/14/19