I need to design an application that makes a histogram that visually shows the distribution of a set of numbers. It has to read in numbers 1-100 and then produce a chart which labels how many of each number is input. So if my input is 1, 23, 17, 13, and 7, then my output would have to be....I dont understand how to implement a loop with an array as well as incorporating everything else, thanks!

1-10 | **
11-19 | ***

## All 13 Replies

Write a for loop
then add an array using the loop variable inside the loop to index into the array

As of right now, my code looks like this...

`import java.util.Scanner;

``````public class HW
{
public static void main(String []args){

int number;
int anArray;
anArray = new int [10];

for (int i = 0; i < number.size(); i++) {
anArray = number.get(i);
System.out.println(anArray);
}

while ((number<100) && (number>0))
{
System.out.println("Enter some numbers between 1 and 100.");
anArray = scan.nextInt();
}

}
}
``````

I need it print out the asterisks and I dont know how to do that using the loop. I think I get the general idea, but I cant put it all together. your example helped and I get what is going on inside the program, I just dont know how to code it.

print out the asterisks and I dont know how to do that using the loop

Use the System.out.print() method in a loop. It will print its output on one line.
When you want to go to a new line use the println() method.

Hi Atlanta15Braves, looking at your class name I think you have this task as your homework :P so, I don't think it's a good idea to give out the solution to you, but I'll be happy to provide you the guidelines and all you have to do is convert it to Java.

2. Store these numbers in an array
3. Create 10 counters for 1 to 10, 2 to 20, ..., 91 to 100 ranges
4. Initialise these counters to zero
5. Check if a number is between any of the ranges defined in step 3 in a `for` loop
6. Increase the counter for that range by 1 if the number happens to be in a certain range
7. Print out the number of stars you have for all 10 ranges (for example, if 11 to 20 range's counter value is 4, then print 4 stars using a loop)

If the steps are not clear, don't hesitate to ask in more detail.

So as of right no I think I am at step 4. I have initialized and read the numbers and put them in an array. I am not sure how to initialize them to zero or how to check if they are in a range....something like a "while > 0 && < 10" or is there something more efficient I could use.

`import java.util.Scanner;

``````public class HW
{
public static void main(String []args){

//declare variable
int[] anArray;

//setup variable value
anArray = new int [10];

System.out.println("Enter some numbers between 1 and 100.");

for (int i = 0; i < 10; i++) {
System.out.println(i);

anArray[0] = 1-9;
anArray[1] = 10-19;
anArray[2] = 20-29;
anArray[3] = 30-39;
anArray[4] = 40-49;
anArray[5] = 50-59;
anArray[6] = 60-69;
anArray[7] = 70-79;
anArray[8] = 80-89;
anArray[9] = 90-100;

}
}
``````

`}`

how to initialize them to zero

The default values for an int array is 0. You don't have to set it to 0.

how to check if they are in a range

Use conditional tests in an if statement.
`if(a>b && c > d)`

what would I be doing wrong here on the loop, I am getting error messages like crazy and I thought I followed the correct formatting

``````import java.util.Scanner;
public class HW
{
public static void main(String []args){

//declare variable
int[] anArray;
int Number;

//setup variable value
anArray = new int [10];

System.out.println("Enter some numbers between 1 and 100.");

for (int i = 0; i < 10; i++) {
System.out.println(i);

anArray[0] = 1-9;
anArray[1] = 10-19;
anArray[2] = 20-29;
anArray[3] = 30-39;
anArray[4] = 40-49;
anArray[5] = 50-59;
anArray[6] = 60-69;
anArray[7] = 70-79;
anArray[8] = 80-89;
anArray[9] = 90-100;

if (anArray > 0) {
Number = '*';}
else if (anArray > 20) {
Number = '**';}
else if (anArray > 30) {
Number = '***';}
else if (anArray > 40) {
Number = '****';}
else if (anArray > 50) {
Number = '*****';}
else if (anArray > 60) {
Number = '******';}
else if (anArray > 70) {
Number = '*******';}
else if (anArray > 80) {
Number = '********';}
else if (anArray > 90) {
Number = '*********';}
else (anArray > 100) {
Number = 'number is too large';}

System.out.println();

}

}
}
``````

I am getting error messages

Please post the full text of the error messages you need help with.

Where is the code that reads in and saves the input to be shown in the histograms?

I suggest that you do the project in steps:
1) read in and save the input in the array
2) print out the contents of the array using the Arrays class's toString() method to format the array for printing.

When the program does those two steps, look at how to print the array's contents in a histogram.

Norm1, thank you for pointing it out that int array values will always be initialised to 0.

Hi Atlanta15Braves, like Norm1 said, it's beneficial to read the error messages, it will point out mistakes more accurately. In your code following are some errors I could point out.

``````import java.util.Scanner;
public class HW
{
public static void main(String []args){
//declare variable
int[] anArray;
int Number;
//setup variable value
anArray = new int [10];
System.out.println("Enter some numbers between 1 and 100.");
/* The message is printed, but after that the numbers are not read */

for (int i = 0; i < 10; i++) {
System.out.println(i);

/* Not a programming error, but here you are assigning 1-9 = -8 to anArray[0] and so on. */
anArray[0] = 1-9;
anArray[1] = 10-19;
anArray[2] = 20-29;
anArray[3] = 30-39;
anArray[4] = 40-49;
anArray[5] = 50-59;
anArray[6] = 60-69;
anArray[7] = 70-79;
anArray[8] = 80-89;
anArray[9] = 90-100;
/* Here an array is compared with a number which is invalid and would never work. If you want to compare an element in an array, you must specify an index inside square bracket, for example, anArray[0] or anArray[1].*/
if (anArray > 0) {
/*The Number variable is of type int, and here a character value is assigned to it, so it will cause an error*/
Number = '*';}
else if (anArray > 20) {
Number = '**';}
else if (anArray > 30) {
Number = '***';}
else if (anArray > 40) {
Number = '****';}
else if (anArray > 50) {
Number = '*****';}
else if (anArray > 60) {
Number = '******';}
else if (anArray > 70) {
Number = '*******';}
else if (anArray > 80) {
Number = '********';}
else if (anArray > 90) {
Number = '*********';}
else (anArray > 100) {
Number = 'number is too large';}
System.out.println();
}
}
}
``````

Norm1 has told you the best thing to do. Read about arrays. Break down your entire problem into small sub-problems. First, you can try reading the input, storing it in an array, and displaying it. Understand the basics and programming will be really fun. When you are done with the first part, we can look at implementing the histogram logic. All the best.

Ok, so I thought I had it down with all of the new parts I have added in, but I am getting an uninitialized variable for my group variable and I cant figure out what is going on, any sugggestions?

```````Inline Code Example Here`import java.util.Scanner;
public class HW
{
public static void main(String []args){

//declare variable
String input;
Double group;
int Number;
int[] anArray;
group = (input - 1) /10;
group ++;

//setup variable value
anArray = new int [10];

Scanner user_input = new Scanner (System.in);

System.out.println("Enter some numbers between 1 and 100.");
System.out.println("Signal the end by entering a number outside of that range.");
input = user_input.next();

for (group = 0; group < 10; group++)
for (int count = 0; count < group; count++)
System.out.println("*");

for (int i = 0; i < 10; i++) {
System.out.println(i);

anArray[0] = 10;
anArray[1] = 20;
anArray[2] = 30;
anArray[3] = 40;
anArray[4] = 50;
anArray[5] = 60;
anArray[6] = 70;
anArray[7] = 80;
anArray[8] = 90;
anArray[9] = 100;

if (anArray[0] > 0) {
Number = '*';
if (anArray[0] > 10) {
Number = '*';
if (anArray[0] > 0) {
Number = '*';
if (anArray[0] > 0) {
Number = '*';
if (anArray[0] > 0) {
Number = '*';
if (anArray[0] > 0) {
Number = '*';
if (anArray[0] > 0) {
Number = '*';
if (anArray[0] > 0) {
Number = '*';
}
}
}
``````

getting an uninitialized variable

Give the variable a value when you define it.

When you get an error message you should copy and paste here the full text. Just copying part of the message can leave off valuable information.

The code has many missing }s. The if statements at the end of the code all have {s without the matching }s

The code you've posted is trying to do too many things all at once. I suggest again that you do one thing at a time. When you get that one step done, then try adding the next step.
The first step should be getting the user's input into an array. And print it to see what is there.
When you can do that then add the next step.

how are the counters created in this program. you are saying if the counter = something that have it spit out the *, but I am not sure how to do the counter part.

I am getting an error message at the spot where I am trying to user input using the group function. It is saying incompatible types since one is string and one is an int, any suggestions on fixing that?

``````while ((input > 0 && input < 100)) {
System.out.println("Enter some numbers between 1 and 100.");
System.out.println("Signal the end by entering a number outside of that range.");
input = user_input.next();  <---------------------- this part
}
group = (input - 1) /10;
group ++;

for (group = 0; group < 10; group++)
for (int count = 0; count < group; count++)
System.out.println("*");
``````
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.