We Value Your Privacy

We and our partners use technology such as cookies on our site to personalise content and ads, provide social media features, and analyse our traffic. Click below to consent to the use of this technology across the web. You can change your mind and change your consent choices at anytime by returning to this site.

Update Consent
Loading ...

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.

Loading ...
  1. 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};

  2. 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.

  3. 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;

  4. 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.

  5. 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 }

  6. 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.

  7. 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 }

  8. 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(" ");

  9. 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.

  10. 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();

  11. 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.

  12. Tip

    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.

Loading ...

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.

Loading ...