Results 1 to 5 of 5
  1. #1
    cn23271 is offline Member
    Join Date
    Jan 2011
    Posts
    3
    Rep Power
    0

    Default problem with array error index out of bounds exception

    Hello dear java users,

    I have programmed this array and cannot find what makes the arrays out of bounds.

    Any help will be appreciated.

    class DivideyVenceras {



    public char tablero[][];

    int dimension;


    public static void main(String[]args){

    DivideyVenceras t = new DivideyVenceras();

    int x=0;
    int y=0;


    t.trimino ( 0, 0, x, y, 4);
    t.imprimeTablero(0, 0, 4);




    }

    public void trimino ( int x0, int y0, int x, int y, int dimension){

    int dim = dimension/2,
    x5,
    y5,
    x1=0,
    y1=0,
    x2=0,
    y2=0,
    x3=0,
    y3=0,
    x4=0,
    y4=0;
    dimension=2;

    tablero = new char [dimension][dimension];

    if ( dimension == 2 ) { /* tablero 2x2 */
    /* encontrar y dibujar trimino */
    if ( x0 == x && y0 == y ){
    /* coordenadas de subtablero 1 */
    tablero[x0][y0] = '#';
    tablero[x0+1][y0] = caracter();
    tablero[(x0)][y0+1] = caracter();
    tablero[(x0+1)][(y0+1)] = caracter();

    }

    else
    if(x0 == x && y0 != y){
    /* coordenadas de subtablero 2 */
    tablero[x0][y0] = caracter();
    tablero[(x0)][y0+1] = '#';
    tablero[x0+1][y0] = caracter();
    tablero[(x0+1)][(y0+1)] = caracter();
    }


    else

    if ( x0 != x && y0 == y ){
    /* coordenadas de subtablero 3 */
    tablero[x0][y0] = caracter();
    tablero[x0][y0+1] = caracter();
    tablero[(x0+1)][y0] = '#';
    tablero[(x0+1)][(y0+1)] = caracter();
    }
    else{
    /* coordenadas de subtablero 4 */
    tablero[x0][y0] = caracter();
    tablero[x0+1][y0] = caracter();
    tablero[(x0)][y0+1] = caracter();
    tablero[(x0+1)][(y0+1)] = '#'; }}

    if ( dimension > 2){
    /* tablero mayor que 2x2*/
    /* encontrar y dibujar trimino*/

    x5= x0 + dim;
    y5= y0 + dim;

    if ( x <5 && y < y5){
    /* coordenadas de x e y en subtablero 1*/


    x1=x;
    y1=y;
    x2=x5-1;
    y2=y5;
    x3=x5;
    y3=y5-1;
    x4=x5;
    y4=y5;

    tablero[x][y] = '#';
    tablero[x2][y2] = caracter();
    tablero[(x3)][y3] = caracter();
    tablero[(x)][(y5)] = caracter();

    }

    else

    if(x< x5 && y>=y5){
    /* coordenadas de subtablero 2 */


    x1 = x5-1;
    y1 = y5 -1;
    x2 = x;
    y2 = y;
    x3 = x5;
    y3 = y5 -1;
    x4=x5;
    y4=y5;

    tablero[x1][y1] = caracter();
    tablero[(x)][y] = '#';
    tablero[x3][y3] = caracter();
    tablero[(x4)][(y5)] = caracter(); }

    else

    if ( x >= x5 && y < y5 ){
    /* coordenadas de subtablero 3 */

    x1= x5-1;
    y1=y5-1;
    x2=x5-1;
    y2=y5;
    x3=x;
    y3=y;
    x4=x5;
    y4=y5;

    tablero[x1][y1] = caracter();
    tablero[x2][y2] = caracter();
    tablero[(x)][y] = '#';
    tablero[(x4)][(y5)] = caracter();
    }

    else {
    /* coordenadas de subtablero 4 */

    x1= x5-1;
    y1=y5-1;
    x2=x5-1;
    y2=y5;
    x3=x5;
    y3=y5-1;
    x4=x;
    y4=y;

    tablero[x1][y1] = caracter();
    tablero[x2][y2] = caracter();
    tablero[(x3)][y3] = caracter();
    tablero[(x)][(y)] = '#'; }}}

  2. #2
    toadaly is offline Senior Member
    Join Date
    Jan 2009
    Posts
    671
    Rep Power
    6

    Default

    The code does not compile, you haven't used code tags, and you haven't provided a stack trace. No-one's going to help you if you just throw a bunch of broken code at us and ask us to fix it without doing your part.

  3. #3
    cn23271 is offline Member
    Join Date
    Jan 2011
    Posts
    3
    Rep Power
    0

    Default Code tags

    Well this is a forum for java beginners. My program seems to be ok in eclipse.
    What do you mean with the code tags?

  4. #4
    Vase is offline Member
    Join Date
    Jan 2011
    Location
    Gainesville, FL
    Posts
    45
    Rep Power
    0

    Default

    [code]These are code tags.[/code]

    Java Code:
    They
       Make
          Code
             Readable

  5. #5
    cn23271 is offline Member
    Join Date
    Jan 2011
    Posts
    3
    Rep Power
    0

    Default program with code tags

    class DivideyVenceras {



    public char tablero[][];
    /*this array is a board to detect triminos, triminos
    triminos are 3 occupied units in board, say (x0, y0)=1,
    (x0, y1)=1, (x1, y1)=1, and the unit left has the simbol
    # as fix variable and would be in (x1, y0)=#
    the reference variable are x, y for the location of symbol
    reference #*/


    int dimension;
    /*shows dimension of the matrix*/

    public static void main(String[]args){

    DivideyVenceras t = new DivideyVenceras();

    int x=0;
    int y=0;


    t.trimino ( 0, 0, x, y, 4);
    /* calls method trimino with values x0=0, y0=0,
    dimension =4, that means matrix is 4x4*/
    t.imprimeTablero(0, 0, 4);
    /* method for printing board*/




    }

    public void trimino ( int x0, int y0, int x, int y, int dimension)/* method for finding trimino, depending on the coordinates and
    the dimension, the method fills the board with triminos*/

    {

    int dim = dimension/2,
    x5,
    y5,
    x1=0,
    y1=0,
    x2=0,
    y2=0,
    x3=0,
    y3=0,
    x4=0,
    y4=0;
    dimension=2;
    /* Starting with trivial case dimension of matrix 2 x2*/
    tablero = new char [dimension][dimension];

    if ( dimension == 2 ) { /* tablero 2x2 */
    /* encontrar y dibujar trimino */
    if ( x0 == x && y0 == y ){
    /* coordinates of subboard above left subtablero 1 */
    tablero[x0][y0] = '#';
    tablero[x0+1][y0] = caracter();
    tablero[(x0)][y0+1] = caracter();
    tablero[(x0+1)][(y0+1)] = caracter();

    }

    else
    if(x0 == x && y0 != y){
    /* coordinates of subboard above right de subtablero 2 */ tablero[(x0)][y0+1] = '#';
    tablero[x0+1][y0] = caracter();
    tablero[(x0+1)][(y0+1)] = caracter();
    }


    else

    if ( x0 != x && y0 == y ){
    /* coordinates of subboard below left de subtablero 3 */
    tablero[x0][y0] = caracter();
    tablero[x0][y0+1] = caracter();
    tablero[(x0+1)][y0] = '#';
    tablero[(x0+1)][(y0+1)] = caracter();
    }
    else{
    /* coordinates subboard below right subtablero 4 */
    tablero[x0][y0] = caracter();
    tablero[x0+1][y0] = caracter();
    tablero[(x0)][y0+1] = caracter();
    tablero[(x0+1)][(y0+1)] = '#'; }}

    if ( dimension > 2){
    /* matrix with dimension bigger than 2x 2 */
    /* encontrar y dibujar trimino*/

    x5= x0 + dim;
    y5= y0 + dim;

    if ( x <5 && y < y5){
    /* coordenadas de x e y en subtablero 1*/


    x1=x;
    y1=y;
    x2=x5-1;
    y2=y5;
    x3=x5;
    y3=y5-1;
    x4=x5;
    y4=y5;

    tablero[x][y] = '#';
    tablero[x2][y2] = caracter();
    tablero[(x3)][y3] = caracter();
    tablero[(x)][(y5)] = caracter();

    }

    else

    if(x< x5 && y>=y5){
    /* coordenadas de subtablero 2 */


    x1 = x5-1;
    y1 = y5 -1;
    x2 = x;
    y2 = y;
    x3 = x5;
    y3 = y5 -1;
    x4=x5;
    y4=y5;

    tablero[x1][y1] = caracter();
    tablero[(x)][y] = '#';
    tablero[x3][y3] = caracter();
    tablero[(x4)][(y5)] = caracter(); }

    else

    if ( x >= x5 && y < y5 ){
    /* coordenadas de subtablero 3 */

    x1= x5-1;
    y1=y5-1;
    x2=x5-1;
    y2=y5;
    x3=x;
    y3=y;
    x4=x5;
    y4=y5;

    tablero[x1][y1] = caracter();
    tablero[x2][y2] = caracter();
    tablero[(x)][y] = '#';
    tablero[(x4)][(y5)] = caracter();
    }

    else {
    /* coordenadas de subtablero 4 */

    x1= x5-1;
    y1=y5-1;
    x2=x5-1;
    y2=y5;
    x3=x5;
    y3=y5-1;
    x4=x;
    y4=y;

    tablero[x1][y1] = caracter();
    tablero[x2][y2] = caracter();
    tablero[(x3)][y3] = caracter();
    tablero[(x)][(y)] = '#'; }}}

Similar Threads

  1. [SOLVED] Array index out of bounds exception
    By sruthi_2009 in forum New To Java
    Replies: 6
    Last Post: 03-23-2014, 10:16 AM
  2. Help Array Index out of bounds exception
    By star400040 in forum New To Java
    Replies: 2
    Last Post: 12-10-2010, 10:24 PM
  3. Array Index Out Of Bounds Exception
    By manowar689 in forum New To Java
    Replies: 3
    Last Post: 06-18-2010, 11:25 PM
  4. array Index out of Bounds exception== 0
    By Allgorythm in forum New To Java
    Replies: 6
    Last Post: 02-11-2010, 04:02 PM
  5. Array Index Out of Bounds Exception
    By kool001 in forum New To Java
    Replies: 1
    Last Post: 12-03-2009, 07:42 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •