-1

I have big trouble with this exception. I'm newbie to Java programming (and programming as well), so.. Why, when second class is returning two arrays exception appears? In addition, when only one array is returning (f.e "return jakob;"), everything works ok. First class:

package mainpackage;

public class GraWojna {
int[] jakob = new int[24];
int[] gracz = new int[24];
public void Wojna() {

    System.out.println("tasowanie kart...");
    // kart w talii jest 48, bo 4* 12.. true?
    WojnaTasowanie tas = new WojnaTasowanie();

    tas.Tasowanie(jakob, gracz);
    int licznik = 0;
    while (licznik<23) {
        System.out.println("jakob:" + jakob[licznik]);
        licznik++;
    }


}

}

Second class:

package mainpackage;

import java.util.Random;


public class WojnaTasowanie {


int[] Tasowanie (int jakob[], int gracz[]) {
    int jakb[] = new int[23];
    int grcz[] = new int[23];
     Random generator = new Random();
     int licznik = 0;
     int[] Pula = new int[11];

    while (licznik<11){

        Pula[licznik] = 4;
        licznik++;
     }   
    licznik = 0;

        int passa = 0;
        int passb = 0;
    while (licznik<22) {

        passa = 5;
        passb = 5;
        System.out.println("step ----");

        while (passa<8){
        int bekaxd = generator.nextInt(11);


        if (Pula[bekaxd]>0) {
            Pula[bekaxd]--;
            passa = 15;
            jakob[licznik] = bekaxd;
        }



        while (passb<8){
            bekaxd = generator.nextInt(11);
            gracz[licznik] = bekaxd;
            System.out.println("licznik:" + licznik + "gracz: " + gracz[licznik] + "pula" + Pula[gracz[licznik]]);
            if (Pula[gracz[licznik]]>0) {
                Pula[gracz[licznik]]--;
                passb = 15;
            }

        }
        licznik++;

        }


    }

    return Tasowanie(jakob, gracz);

}

}

Stack trace:

 Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 25
at mainpackage.WojnaTasowanie.Tasowanie(WojnaTasowanie.java:51)
at mainpackage.WojnaTasowanie.Tasowanie(WojnaTasowanie.java:76)
at mainpackage.WojnaTasowanie.Tasowanie(WojnaTasowanie.java:76)
at mainpackage.GraWojna.Wojna(GraWojna.java:12)
at mainpackage.Main.main(Main.java:16)

Thank you in advance!

Ziemowit Wójcik
  • 25
  • 1
  • 1
  • 3
  • 6
    You need to post your stack trace... – redFIVE Aug 27 '13 at 16:36
  • 1
    there are 52 cards in a deck, btw. Each colour has 13 cards. – abc Aug 27 '13 at 16:38
  • Ok, done, sorry, i've never had opportunity to ask a questions here. Kostek - indeed, i was so absorbed "uaa, i'm writing my first program!11" that i made this very silly fault.. – Ziemowit Wójcik Aug 27 '13 at 16:42
  • learn to use proper typesafe lists instead of raw arrays and you won't have these issues –  Aug 27 '13 at 16:45
  • It's common to use camel case in variable names, them maybe you could change you variable "Pula" to "pula". – guisantogui Aug 27 '13 at 16:51
  • 1
    In the last line of your `Tasowanie` function, you call the same function again. This is a recursive function, which may be exactly what you want but it also makes it harder to understand and debug. I also don't see how your recursion would ever terminate (except with an error or stack overflow). – jfrank Aug 27 '13 at 16:56

5 Answers5

1

ArrayIndexOutOfBoundsException is thrown because-

Thrown to indicate that an array has been accessed with an illegal index. The index is either negative or greater than or equal to the size of the array.

I cannot debug your class right now, but here are some examples why a program would throw this exception-

Say you have-

int[] integers = new int [2];

// This is fine since our array has two indexes which are 0 and 1 with length being 2    
integers[0] = 2;  

// However this not fine since our array has a length of 2. So there's no such index 2 so saying integers[2] would throw that error
integers[2] = 3;

// Likewise this is ok
System.out.println(integers[1]);

// But this will throe the same exception
System.out.println(integers[3]);

So, debug your program and find where you have such scenario in your program so you can fix them.

Sajal Dutta
  • 7,258
  • 25
  • 33
0

IndexOutOfBoundsException occours when you try to access a position in an Array that doesn't exists:

Eg.

int[] a = {7,12,5}

int ac = a[3] //Here index out of Exception

them you must to find where you are trying to access an invalid position.

guisantogui
  • 3,608
  • 6
  • 45
  • 83
0

The Pula array has 11 elements, but the indexes are 0 .. 10. The random generator is making the int 11, so when used here (or any place the random number is used as an index):

if (Pula[bekaxd]>0) {

it throws the error.

Wayne Weibel
  • 833
  • 1
  • 14
  • 21
  • I don't think that's the problem. random.nextInt(11) returns a value between 0 and 10 (upper bound is exclusive). – jfrank Aug 27 '13 at 16:52
  • Ah, yes, very true, but there are random numbers being made and stored in several arrays then manipulated and used as indexes (rather confusing code). It could be a 0 that is decremented to -1, now that I look even closer at the code. – Wayne Weibel Aug 27 '13 at 17:04
0

It's a little hard to follow, but here is what I think is going wrong. Inside your loop that starts while (licznik< 22), there is another loop that starts while (passa < 8). Inside that inner loop, you have licznik++. It's my guess that this inner loop runs too may times, and results in a value for licznik that is greater than the size of the jakob array.

jfrank
  • 723
  • 3
  • 9
0

you have licznik++; inside the wrong loop, move it down 2 lines and out of the inner loop.

 while (passb<8){
            bekaxd = generator.nextInt(11);
            gracz[licznik] = bekaxd;
            System.out.println("licznik:" + licznik + "gracz: " + gracz[licznik] + "pula" + Pula[gracz[licznik]]);
            if (Pula[gracz[licznik]]>0) {
                Pula[gracz[licznik]]--;
                passb = 15;
            }

        }


        }
      licznik++;
azzurroverde
  • 537
  • 5
  • 17
  • That will solve the error you have, the ArrayoutofIndexBoundaryException , but it will generate new ones along the line, mainly an infinite loop. I guess you have to pay attentions to the variables you are using, is licznik++ correct? Shouldn't you change passb instead? Maybe you meant to write passb++ instead of liczink++. I don't understan the logic of your program, you will have to know :) – azzurroverde Aug 27 '13 at 17:02
  • i've just noticed, but didn't helped – Ziemowit Wójcik Aug 27 '13 at 17:05
  • There are two loops with the condition passb<8 and passa<8 and you get out of the loop only if you assign passb=15 or passa=15. That happens if Pula[gracz[licznik]]>0 or if Pula[bekaxd]>0 but it never is, at least not according to my run. – azzurroverde Aug 27 '13 at 17:18
  • Ok, I guess I see where the problem is: you have a main while loop that checks that licznik<11, but then you increment licznik inside an inner loop without any check, so it can increase without control. How to fix it depends on what you want the program to do ... – azzurroverde Aug 27 '13 at 17:29
  • Hmm, yep, but licznik is a counter, which i reset after first loop - i don't understand you a bit.. – Ziemowit Wójcik Aug 27 '13 at 18:17
  • Okay, I understood, everything works fine, thanks! – Ziemowit Wójcik Aug 27 '13 at 18:32