title: Print horizontal histogram for letters in file. How to scale it? Java
body: <p>I would like to count the occurences of letters in a text file and draw a horizontal histogram. However, the longest bar should have the length of <code>50</code> stars. It's not a problem, when I have, the number less than <code>50</code>, but how to scale it so I have everything on a histogram? </p>
  
  <p>I came up with this formula: <code>50 - 50 * histogram[i] / maxnum</code> where <code>50</code> is the maximum length of the bar, <code>histogram[i]</code> is the number of occurences of the <code>i-th</code> letter in an alphabet and <code>maxnum</code> is the number representing the number of occurences of the  most common letter in my file. </p>
  
  <pre><code>int[] histogram = new int[26];     
  String alphabeth = "abcdefghijklmnopqrstuvwxyz";
  </code></pre>
  
  <p>Here's how I build the histogram:</p>
  
  <pre><code>while((line = bfr.readLine()) != null){
       for (int i =0;i&lt; line.length();i++){
            int index = alphabeth.indexOf(line.charAt(i));
            if (index != -1)
              histogram[index]++;  
       }
  }
  </code></pre>
  
  <p>I look for the <code>maxval</code>:</p>
  
  <pre><code>int maxnum = histogram[0];
          for (int p=1; p&lt;histogram.length;p++){
              if (maxnum &lt; histogram[p]){
                  maxnum = histogram[p];
              }
          }
  </code></pre>
  
  <p>And that's how I draw it:</p>
  
  <pre><code>for (int i=0; i&lt;alphabeth.length(); i++){
              System.out.print(alphabeth.charAt(i) + " ");
              int count = histogram[i];
                  for (int k=0; k&lt;50-50*histogram[i]/maxval; k++){
                      System.out.print("*");
                  }
          }
  </code></pre>
  
  <p>Is it right? </p>