java - Array Index out of bounds think there is a specfic line -
exception in thread "main" java.lang.arrayindexoutofboundsexception: 100 @ ham.main(ham.java:34)
line 34 on console says if (h[c] == 1)
i wrote code generate hamming code..i getting javaindexoutbounds exception..i gave absurdly large array sizes counter tht..still not working! array outbounds thou there plenty of space array
the line 27 might mistake...checking c
import java.util.*; public class ham { public static void main(string ar[]) { scanner s = new scanner(system.in); system.out.println("input no. of bits"); int n = s.nextint(); int a[] = new int[100]; // user's input int h[] = new int[100]; // hamming code array system.out.println("i/p data"); int = 1, j = 1, pb = 1; (i = 1; < n + 1; i++) a[i] = s.nextint(); = 1; while (i < n + 1) { if (j == pb) // if index parity bit leave { j++; pb = pb * 2; } else { h[j] = a[i]; j++; i++; } // else copy data bits a[] h[] } int c = 0, counter = 0; // fill parity bits(k) (int k = 1; k <= j; k = 2 * k) { c = k; while (c <= j) // 'j' position of last data bit in h[] { (c = k; c < (c + k); c++) { if (h[c] == 1) // line 34 counter++; } c = c + k + 1; } if (counter % 2 == 0) h[k] = 0; else h[k] = 1; } system.out.println("hamming code is"); (i = 1; <= j; i++) system.out.print(h[i] + " "); }
}
the if (h[c] == 1)
test causing exception.
your h
array has fixed size of 100
, maximum value of c
seems depend on user's input, n
. need figure out how dynamically determine array sizes, depending on user input.
Comments
Post a Comment