0

Write a program Example.java to compute data to construct a histogram of integer values read from a file. A histogram is a bar chart in which the length of each bar gives the number of items that fall into a certain range of values, usually called a bin. You won’t actually be drawing a bar chart, but instead will print out the size of each bin.

Your program should take four command line arguments:

  1. The name of a file containing an array of integers
  2. An integer b giving the number of bins to sort into.
  3. A integer min giving the lowest number in the smallest bin.
  4. An integer s giving the size (number of distinct integers) in each bin. You can assume (without checking) that b > 0 and s > 0.

Divide the range of values of interest into b bins of size s. Count the number of values from the file that fall into each bin. Also count the number of values that are completely below or above the range.

For example, given this test file data1:"05X/data1"

    1 15
    2 18 11 -101 51 92 53 45 55 52 53 54 55 56 5 -2

The output of java Example data1 10 -10 7 should be

    x < -10: 1
    -10 <= x < -3: 0
    -3 <= x < 4: 1
    4 <= x < 11: 1
    11 <= x < 18: 1
    18 <= x < 25: 1
    25 <= x < 32: 0
    32 <= x < 39: 0
    39 <= x < 46: 1
    46 <= x < 53: 2
    53 <= x < 60: 6
    x >= 60: 1

My code below is able to print out the first line of the output. The for loop to print out the range min <= x < max : bin keeps getting the exception

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 1 out of bounds for length 1 at Example.main(Example.java:49)

What syntax is wrong here? Please help

    class Example {
      public static void main (String argv[]) throws IOException {
        if (argv.length != 4)
            usage ();
        int[] is = {0};
        int b = 0;
        int min = 0;
        int s = 0; 
        try {
            is = readIntArray(argv[0]);
            b = Integer.parseInt (argv[1]);
            min = Integer.parseInt (argv[2]);
            s = Integer.parseInt(argv[3]);
        } catch (NumberFormatException e) {
            usage();
        }

        int max = b * s + min;
        int [] count = {0};
        for (int i = 0; i < is.length; i++)
        if (is[i] < min){
            count[0]++;
        } else if (i >= max) {
            count[b+1]++;
        } else {
            int n = min;
            int index = 0;
            while (i < n + s){
                n += s;
            index++;
            count[i]++;
            }
        }
        System.out.println("x < " + min + ": " + count[0]);
        for (int i = 1; i <= b; i++){
          int low = s * i + min;
          int high = s * (i + 1) + min;
        System.out.println(low + " <= x < " + high + ": " + count[i]);
        }
        System.out.println("x >= " + max + ": " + count[b + 1]);
      }
miiiii
  • 1,429
  • 1
  • 15
  • 27
Vincent
  • 19
  • 2
  • 1
    Arrays are not dynamically resizeable. `int [] count = {0};` the length of this array is `1` and can **only** be indexed as `count[0]`. `count[b+1]` does not exists unless that balue of `b` is `-1` – Scary Wombat Oct 07 '19 at 06:20
  • Possible duplicate of [What causes a java.lang.ArrayIndexOutOfBoundsException and how do I prevent it?](https://stackoverflow.com/questions/5554734/what-causes-a-java-lang-arrayindexoutofboundsexception-and-how-do-i-prevent-it) – Andreas Oct 07 '19 at 06:21

1 Answers1

0

Your count array has a length of 1: int [] count = {0}; but you're trying to access higher indices:

if (is[i] < min){
    count[0]++;
} else if (i >= max) {
    count[b+1]++; //here
} else {
    int n = min;
    int index = 0;
    while (i < n + s){
        n += s;
        index++;
        count[i]++; // and here
    }
}

Since indices other that 0 don't exist in the array, they're out of bounds, so you're getting an exception.

Malt
  • 25,324
  • 9
  • 56
  • 86