Java out-of-bounds error -
i keep getting out of bounds error. want check duplicates. there more after this, right thats important says i'm getting outofboundsexception
arraylist
, index:1 size:1
arraylist lottery = new arraylist(); (int n = 0; n < 4;) { int number = 0; int first = 0; int value = 0; boolean found = false; first = (int) (math.random() * 42 + 1); lottery.add(first); count[first]++; (int = 1; < 6;) { number = (int) (math.random() * 42 + 1); (int k = 0; k < 6;) { // here value = lottery.get(k); if (value == number) { found = true; } else { found = false; } if (found == true) { number = (int) (math.random() * 42 + 1); } else { k++; } } system.out.println("number " + (i + 1) + ": " + number); if (found == false) { lottery.add(number); count[number]++; i++; } } }
i assumed count
array want check 2 mistakes noticed value parameter never changed stay =0
, in code
number = (int) (math.random() * 42 + 1); if (value == number) { found = true;
number never equal 0
if (found == false) { lottery.add(number); count[number]++; i++; }
will executed , adding new element lottery happen twice in each loop , there no n++
basicaly big loop never end , code
first = (int) (math.random() * 42 + 1);
are sure count length 42 because if less cause indexoutofboundsexception
, if more 42
not checking elements in :) hope helped
Comments
Post a Comment