How to display a vertical histogram in Java

If you have a set of values and want to display a vertical histogram of them in a Java application, you can do so using loops. By first determining the maximum value in your set, then printing a line of the histogram from this value down to one, you can create a text string output you can display in the standard output console, a Graphical User Interface or any other output stream you choose. Your input values can be numeric types such as integers and can be stored in a structure such as an array.

Prepare your histogram values. The following code demonstrates a short series of primitive type integer values stored in an array structure: int[] values = {5, 3, 7, 4, 6};

Your values can be stored in another Java storage structure of your choice, as long as you can iterate through the structure in a loop. You can use as many values as your project requires.

Find the maximum value in your set. To display the vertical histogram, you will need to work from the top down, so the first step is to find the maximum value. The following generic for loop structure demonstrates storing the maximum value from the array in an integer variable: int maxValue=0; for(int i : values) if(i>maxValue) maxValue=i;

The loop iterates through the array, checking each value in turn. When it encounters a value that is greater than the greatest value encountered so far, it updates the variable accordingly. When the loop finishes executing, the integer variable will store the maximum value in the array.

Loop through the values from the greatest down to one. You will need to print your histogram from the top down, so use a loop with the following structure: for(int j=maxValue; j>0; j--){ //go through values }

This loop starts at the maximum value and counts down to one, by testing that the index counter is greater than zero and decrementing by one each time the loop iterates. You will need to add more code inside this loop.

Loop through your values. Inside the loop counting from the maximum value down, add another loop to iterate through the value array as follows: for(int k=0; k<values.length; k++){ //print values }

Inside this loop, add a conditional statement checking whether the value is at least the current value in the outer loop: if(values[k]>=j) System.out.print("|"); else System.out.print(" ");

If the value is at least the value in the outer loop, the program will print a character, in this case the bar character. If the value is not large enough, the program will print a space character, keeping the lines in the vertical histogram aligned correctly.

Enter new lines to your histogram. After the loop in which you carried out the conditional printing operation, but still inside the loop counting down from the maximum value, enter a new line: System.out.println();

The output will place the output of the next loop iteration on a new line so that your histogram displays correctly. Save and run your program. For the example array integers, it should display five bar characters in the first column, three in the second, and so on.


You can use a GUI or other output method for your histogram, as well as altering the display within it, for example using images instead of characters.


Using embedded loops makes accidental infinite loops more likely to occur.

Cite this Article A tool to create a citation to reference this article Cite this Article

About the Author

Sue Smith started writing in 2000. She has produced tutorials for companies including Apex Computer Training Software and articles on computing topics for various websites. Smith has a Master of Arts in English language and literature, as well as a Master of Science in information technology, both from the University of Glasgow.