Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Pleas help! array index out of bouderies

  1. #1
    Junior Member
    Join Date
    May 2013
    Location
    Tilburg
    Posts
    26
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Exclamation Pleas help! array index out of bouderies

    When I execute my program it gives an error :

    java.lang.ArrayIndexOutOfBoundsException: 6
    at Field.numNeighbours(GameOfLife.java:131)
    at Field.generationNextCalc(GameOfLife.java:164)
    at GameOfLife.play(GameOfLife.java:67)
    at GameOfLife.main(GameOfLife.java:17)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Nativ e Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknow n Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Un known Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at edu.rice.cs.drjava.model.compiler.JavacCompiler.ru nCommand(JavacCompiler.java:272)

    I really don't see it and I hope you can help me.

    My code:

    import java.util.*;
    import java.io.*;
     
    class GameOfLife{  // main class
      Field field = new Field();
      Scanner scanner;
      File source;
      String filename = "GameOfLifeInput.txt";
      char[] charArray;
     
      public static void main(String[]args){ // starts up program
        new GameOfLife().play();
      } // end of method
     
      void readInitial(String filename){ // reads input from file
        try{
          source = new File(filename);
          scanner = new Scanner(source);
          String line = null;
          int row = 0;
          int col = 0;
          LineNumberReader lnr = new LineNumberReader(new FileReader(new File(filename)));
          lnr.skip(Long.MAX_VALUE);
          lnr.getLineNumber();
          Cell[][] cells = new Cell[lnr.getLineNumber()][];
     
          while (scanner.hasNext()){
            line = scanner.nextLine();
            char[] charArray = line.toCharArray();
            cells[row] = new Cell[charArray.length];
            for (Character c : charArray){
              Cell cell = new Cell();
              if (c == ' '){
                cell.setAlive(false);
              }else if (c == '*'){
                cell.setAlive(true);
              }
              cells[row][col] = cell;
              ++col;
            }
          ++row;
          col = 0;
          field.setField(cells);
          }
          //field.setField(cells);
        }catch(FileNotFoundException e){
          System.out.println("Could not find or open file due to \n"+e);
        }catch (IOException e) {}
     
      } // end of method 
     
      void play(){ //Asks user for number of generations and execute those generations with brakes of 2 secs in between them
        try{
          int numberOfGenerations = 0;
          int i = 0;
          scanner = new Scanner(System.in);
          System.out.println("How many generations do you want to see?");
          numberOfGenerations = scanner.nextInt();
          readInitial(filename);
          for(i = 0; i < numberOfGenerations; i++){
            field.print();
            field.generationNextCalc();
            Thread.sleep(2000);
          }
        }catch(InterruptedException e){
        }
      }
    } // end of class
     
    class Field{
      int i, j;
      Cell[][] cells;
     
      void setField(Cell[][] cells){ // method which replaces the empty matrix with the matrix with values read from file
        this.cells = cells;
      }
     
      void drawLine(){ //method to draw lines
        String s = "+";
        for(int i = 1; i < cells[0].length-1; i++){
          s+="--";
        }
        s+="+";
        System.out.println(s);
      } // end of method
     
      public void print(){ //method to print generations
        drawLine();
        for(int i = 1; i < cells.length-1; i++){
          System.out.print("|");
          for(int j = 1; j < cells[i].length-1; j++){
            if(cells[i][j].isAlive()){ 
              System.out.print("*");
            }else{
              System.out.print(" ");
            }
          }
          System.out.println("|");
        }
        drawLine();
     
      } // end of method
     
      int numNeighbours(int i, int j){ // method to count the number of neighbors
        int result = 0;
     
        for(i = 0 ; i<cells[0].length ; i++){
          for(j = 0 ; j<cells.length ; j++){
     
        if(i>0 /*&& j>0 && j<cells.length-1*/){
          if(cells[i-1][j].alive = true){  // Not top and not left and not right
            result++;
          }
        }
        if(i<cells[0].length-1 /*&& j>0 && j<cells.length-1*/){  // Not bottem and not left and not right
          if(cells[i+1][j].alive = true){
            result++;
          }
        }
        if(j>0 /*&& i>0 && i<cells[0].length-1*/){  // Not left and not top and not bottem
          if(cells[i][j-1].alive = true){
            result++;
          }
        }
        if(j<cells.length-1 /*&& i>0 && i<cells[0].length-1*/){  // Not right and not top and not bottem
          if(cells[i][j+1].alive = true){
            result++;
          }
        }
        if(i > 0 && j > 0){  // If cell isn't on the left border and top border
          if(cells[i-1][j-1].alive = true){
            result++;
          }
        }
        if(i > 0 && j < cells.length-1){  // If cell isn't on the right border and top border
          if(cells[i-1][j+1].alive = true){
            result++;
          }
        }
        if(i < cells[0].length-1 && j > 0){  // If cell isn't on the bottem border and not on left border
          if(cells[i+1][j-1].alive = true){
            result++;
          }
        }
        if(i < cells[0].length-1 && j < cells.length-1){  // If the cell isn't on the right border and the bottem border
          if(cells[i+1][j+1].alive = true){
            result++;
          }
        }
          }}
     
        return result;
      } // end of method
     
      void generationNextCalc(){ // method to count the next generation
     
        for (int i = 0; i < cells[0].length-1; i++){
          for (int j = 0; j < cells.length-1; j++){
            int numNeighbours = numNeighbours(i,j);
            if (numNeighbours < 2 || numNeighbours > 3){
              cells[i][j].setAlive(false);
            }else if (numNeighbours == 3){
                cells[i][j].setAlive(true);
            }
          }
        } 
     
        /* (int j = 1; j < cells.length-1; j++){
          for (int i = 1; i < cells[0].length-1; i++){
            cells[i][j] = cells[i][j];
          }
        }*/
      } // end of method
     
    } // end of class
     
    class Cell{
      boolean alive = false;
      GameOfLife character = new GameOfLife();
     
      boolean isAlive(){
        return alive;   
      } // end of method
     
      void setAlive(boolean state){
        alive = state;
      } // end of method
     
    } // end of class


  2. #2
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,537
    My Mood
    Grumpy
    Thanks
    0
    Thanked 165 Times in 156 Posts

    Default Re: Pleas help! array index out of bouderies

    Way too much code. I have no idea which is line 131.
    Improving the world one idiot at a time!

  3. #3
    Junior Member hackthisred's Avatar
    Join Date
    Apr 2012
    Posts
    18
    Thanks
    1
    Thanked 1 Time in 1 Post

    Default Re: Pleas help! array index out of bouderies

    This occurs when you attempt access an array with an illegal index... you are either passing in a negative number or a number larger than your array size.
    f34r th3 kut3 1z

  4. #4
    Junior Member
    Join Date
    May 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Pleas help! array index out of bouderies

    I found the solution of this problem. And I think I have to share this with you. Problem lies here:
    for(i = 0 ; i<cells[0].length ; i++){
    for(j = 0 ; j<cells.length ; j++){
    ...........} }
    You are accessing "cells[i][j]" array using this two loops. But you have cells array of size cells[cells.length][cells[0].length]. If you get my point you better want to replace above two statements with
    for(i = 0 ; i<cells.length ; i++){
    for(j = 0 ; j<cells[0].length; j++){
    ...........} }


    Let me know your results.

    Thanks,
    Ajit

Similar Threads

  1. [SOLVED] My loop skips index[0] of my array
    By Benner in forum What's Wrong With My Code?
    Replies: 4
    Last Post: March 22nd, 2013, 06:09 PM
  2. [SOLVED] Merge two 1D array's into a new 1D array. Trouble appending last index
    By norske_lab in forum What's Wrong With My Code?
    Replies: 1
    Last Post: August 30th, 2012, 12:57 PM
  3. Array Index Out of bounds?
    By blazeking in forum Collections and Generics
    Replies: 1
    Last Post: March 29th, 2012, 01:44 AM
  4. Array index out of bounds
    By fortune2k in forum Collections and Generics
    Replies: 1
    Last Post: November 30th, 2010, 06:11 AM
  5. index of array
    By nasi in forum Java Theory & Questions
    Replies: 1
    Last Post: April 12th, 2010, 02:39 AM