title: Printing a Graph from a char[][] in Java
body: <p>The method below loops through a given <code>int[]</code>, storing both the frequencies of the numbers found in the array along with each different appearing number into a <code>HashMap</code>.</p>
  
  <p>After looping, the <code>HashMap</code> would look something like this (num=frequency):</p>
  
  <pre><code>{0=4, 656=1, 1=1, 2=2, 3=2, 4=3, 8=1, 56=1, 75=1, 12=3, 13=1, 61=1}
  </code></pre>
  
  <p>Following this, the method creates a <code>char[][]</code>, representing designated x &amp; y coords for the size and frequency of each integer. The x axis represents the size of the number, and the y axis represents the number's frequency in the <code>HashMap</code>.</p>
  
  <pre><code>private static void print(int[] intArr) {
      /*
     * Purpose of method is to print:
     * The frequency of each index
     * A graphical histogram of the frequencies of each index
      */
      Map&lt;Integer, Integer&gt; map = new HashMap&lt;&gt;();
      for (int i : intArr)
          // If current index of the array being searched is found in map, +1 to total # of occurrences of that index
          if (map.containsKey(i)) map.put(i, map.get(i) + 1);
          else map.put(i, 1);
  
      // Iterate through all entries of map, printing the key and value of each
      for (Entry&lt;Integer, Integer&gt; entry : map.entrySet()) System.out.println("Frequency of index " + entry.getKey() + " is " + entry.getValue());
      System.out.println();
  
      char[][] graph = new char[Collections.max(map.values()) + 1][map.keySet().size() + 1];
      for (int x = 0; x &lt; graph.length; x++)
          for (int y = 0; y &lt; graph[x].length; y++) {
              graph[x][y] = ' ';
              // if (....) graph[x][y] = '*';
              // else graph[x][y] = ' ';
          }
      // Create borders
      for (int i = 0; i &lt; graph.length; i++) graph[i][0] = '|';
      graph[graph.length - 1][0] = '+';
      for (int i = 1; i &lt; graph[0].length; i++) graph[graph.length - 1][i] = '-';
      // Print graph into console
      for (int x = 0; x &lt; graph.length; x++) {
          for (int y = 0; y &lt; graph[0].length; y++) System.out.print(graph[x][y]);
          System.out.println();
      }
  }
  </code></pre>
  
  <p>The output of the code is like so:</p>
  
  <pre><code>Frequency of index 0 is 4
  Frequency of index 656 is 1
  Frequency of index 1 is 1
  Frequency of index 2 is 2
  Frequency of index 3 is 2
  Frequency of index 4 is 3
  Frequency of index 8 is 1
  Frequency of index 56 is 1
  Frequency of index 75 is 1
  Frequency of index 12 is 3
  Frequency of index 13 is 1
  Frequency of index 61 is 1
  
  |            
  |            
  |            
  |            
  +------------
  </code></pre>
  
  <p>Now the problem I'm having is that I can't seem to figure out how to plot an asterisk at the corresponding x &amp; y coords where they are needed. I want the graph to look like this instead:</p>
  
  <pre><code>|*           
  |     *   *  
  |   **       
  | **   *** **
  +------------
  </code></pre>
  
  <p>What I need is a proper if statement checking if the current coordinate being printed is that of the proper column and row, and if not, then print an empty space. As of now, all the program does is fill in everything with empty spaces. I've tried messing around with the code a lot, but it mostly just ended with bugs and <code>ArrayIndexOutOfBounds</code> exceptions.</p>