JebLab
JebLab

Reputation: 28

Input strings into 2d array

Trying to make a histogram, but the loop I'm trying to use for it is giving me a Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 14 error. I'm trying to get something along the lines of:

*       *               
*       *       *       *       
*       *   *   *       *   *   
*       *   *   *       *   *   
*       *   *   *   *   *   *   
*       *   *   *   *   *   *   *   *
*   *   *   *   *   *   *   *   *   *
*   *   *   *   *   *   *   *   *   *
*   *   *   *   *   *   *   *   *   *
*   *   *   *   *   *   *   *   *   *
*   *   *   *   *   *   *   *   *   *
*   *   *   *   *   *   *   *   *   *
*   *   *   *   *   *   *   *   *   *
-   -   -   -   -   -   -   -   -   -
0   1   2   3   4   5   6   7   8   9

And this is what I have so far:

public static void VerticalHist()
   {
      int max = 0; // initialize max

      int[] count = new int[10]; // make array to find max

      for (int i = 0; i < 100; i++)
      {
         int rand = (int)(Math.random() * ((9 - 0) + 1)); // generate random values

         count[rand]++;
      }

      for (int x : count) // find max 
      {
         if (x > max)
            max = x;
      }

      // System.out.println(max);
      String[][] nums2 = new String[max][10]; // create 2d array for histogram

      for (int x = max; x > 0; x--)
      {
         System.out.println();
         for (int i = 0; i < nums2[x].length; i++)
         {
            if (count[i] > 0)
               nums2[x][i] = "*";
         }
      }

      for (int i = 0; i < max; i++) // print 2d array
      {
         System.out.println();
         for (String n: nums2[i])
         {
            System.out.print(n);
         }
      }

   }

My loop for inserting the * is giving me the error.

for (int x = max; x > 0; x--)
      {
         System.out.println();
         for (int i = 0; i < nums2[x].length; i++)
         {
            if (count[i] > 0)
               nums2[x][i] = "*";
               count[i]--;
            else
               nums2[x][i] = "";
         }
      }

I'm trying to take each row, check each index to see if there needs to be an asterisk, (if so, put an asterisk, if not put a blank space) and do so for each row in the 2d array.

Upvotes: 0

Views: 45

Answers (2)

RaffleBuffle
RaffleBuffle

Reputation: 5455

The first thing is that this line

for (int x = max; x > 0; x--)

should be

for (int x = max-1; x >= 0; x--)

I would then change the logic to decide whether to add a "*" or a " " to this:

if (count[i] > x)
   nums2[x][i] = "*";
else
   nums2[x][i] = " ";

Note that this needs to be a space, not an empty string, otherwise your rows will be compressed.

With these changes you get output like this:

**********
**********
**********
**********
**********
**********
**********
** *******
 * *** ***
 * * * ***
 * *   ** 
 * *    * 
        * 

Which is obviously upside down. You need to change this line:

for (int i = 0; i < max; i++) // print 2d array

to this

for (int i = max-1; i >= 0; i--) // print 2d array

Which produces:

 *  **    
 *  **    
 *  **    
 *  ** *  
 *  ** *  
 * *** * *
 * *** * *
 * *** * *
 ***** ***
****** ***
****** ***
****** ***
**********
**********
**********

Upvotes: 0

Nithin
Nithin

Reputation: 703

I think the problem might be in the for loop definition,

for (int x = max; x > 0; x--)

Can you try changing it to

for (int x = max-1; x >= 0; x--)

The reason is, since you define an array of size max and index starts from zero max is actually outside the array

Upvotes: 1

Related Questions