7 Replies - 915 Views - Last Post: 26 March 2010 - 03:01 AM Rate Topic: -----

#1 co2010k  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 08-January 10

Array Out of Bounds

Posted 25 March 2010 - 11:57 PM

I keep getting Array out of bounds. Can anyone help?

[/codepublic class Life{

	private String[][] grid;
	private String[][] grid2;
	
	public void getFile(FileInput infile){
		grid = new String[20][20];
		blankGrid();
		int n = infile.readInt();
		for(int i = 0; i<n; i++){
			int x= infile.readInt();
			int y= infile.readInt();
			grid[x-1][y-1] = "*";
			}
	
		printGrid();
		infile.close();
		}
	
	public void blankGrid(){
		for(int i = 0; i<20; i++)
			for(int j = 0; j<20; j++)
				grid[i][j]=" ";
		}
		
	public boolean inBounds(int x, int y){
		if(x>0 && x<=20) 
			return true;
		if(y>0 && y<=20)
			return true;
		return false;
		}
		
	public int countNeighbors(int x, int y){
		int count = 0;
		if((inBounds(x,y))&&grid[x-1][y].equals("*"))
			count++;
		if((inBounds(x,y))&&grid[x][y-1].equals("*"))
			count++;
		if((inBounds(x,y))&&grid[x-1][y-1].equals("*"))
			count++;
		if((inBounds(x,y))&&grid[x-1][y+1].equals("*"))
			count++;
		if((inBounds(x,y))&&grid[x+1][y-1].equals("*"))
			count++;
		if((inBounds(x,y))&&grid[x+1][y+1].equals("*"))
			count++;
		return count;
		}
		
	public boolean lives(int x, int y){
		if(countNeighbors(x,y)<2 || countNeighbors(x,y)>4)
			return false;
		if(countNeighbors(x,y)==2 &&countNeighbors(x,y)==3)
			return true;	
			return false;
		}
				
	public void nextGeneration(){
		grid2 = new String[20][20];
		for(int x = 0; x<20; x++){
			for(int y = 0; y<20; y++){
				if(lives(x,y)&& inBounds(x,y))
				grid2[x][y] = "*";
			}
		}
		grid = grid2;
	}
	
	public void printGrid(){
	for(int row = 0; row<20; row++){
		for(int col = 0; col<20; col++){
			System.out.print(grid[row][col]);
			}
			System.out.println();
		}
	}

	public static void main(String[] args){
		Life game = new Life();
		FileInput infile = new FileInput("life100.txt");
		game.getFile(infile);
		for(int i = 1; i<5; i++){
			game.nextGeneration();
			}
			game.printGrid();
		}
	} [code]


Is This A Good Question/Topic? 0
  • +

Replies To: Array Out of Bounds

#2 n8wxs  Icon User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 972
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: Array Out of Bounds

Posted 26 March 2010 - 12:15 AM

Since there are several possibilities, please post the error message(s).
Was This Post Helpful? 0
  • +
  • -

#3 co2010k  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 08-January 10

Re: Array Out of Bounds

Posted 26 March 2010 - 12:18 AM

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -1
at Life.countNeighbors(Life.java:43)
at Life.lives(Life.java:59)
at Life.nextGeneration(Life.java:70)
at Life.main(Life.java:91)
Was This Post Helpful? 0
  • +
  • -

#4 n8wxs  Icon User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 972
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: Array Out of Bounds

Posted 26 March 2010 - 01:02 AM

Could you post "life100.txt" please?
Was This Post Helpful? 0
  • +
  • -

#5 red warrior  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 12
  • Joined: 17-March 10

Re: Array Out of Bounds

Posted 26 March 2010 - 01:09 AM

check , index size and the values u r trying to retrieve...

This post has been edited by red warrior: 26 March 2010 - 01:10 AM

Was This Post Helpful? 0
  • +
  • -

#6 co2010k  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 08-January 10

Re: Array Out of Bounds

Posted 26 March 2010 - 01:09 AM

View Postco2010k, on 25 March 2010 - 11:18 PM, said:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -1
at Life.countNeighbors(Life.java:43)
at Life.lives(Life.java:59)
at Life.nextGeneration(Life.java:70)
at Life.main(Life.java:91)




100

1 3

1 7

1 8

1 11

1 12

1 17

2 1

2 5

2 8

3 7

3 13

3 16

3 20

4 1

4 5

4 15

4 17

4 18

5 1

5 9

5 13

5 15

5 16

6 1

6 3

6 5

6 8

6 14

6 18

7 12

7 15

7 16

7 19

8 5

8 11

8 14

8 15

9 7

9 12

9 13

9 15

9 18

9 20

10 7

10 9

10 14

10 15

11 4

11 11

11 20

12 7

12 8

12 10

12 11

12 18

13 1

13 3

13 13

14 1

14 2

14 8

14 9

14 15

14 16

15 8

15 10

15 13

15 14

15 17

15 19

16 1

16 3

16 7

16 9

16 11

16 12

16 17

16 18

17 3

17 4

17 19

18 3

18 8

18 12

18 14

18 20

19 1

19 6

19 7

19 12

19 13

19 14

19 15

20 4

20 5

20 7

20 9

20 12

20 17

20 18
Was This Post Helpful? 0
  • +
  • -

#7 n8wxs  Icon User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 972
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: Array Out of Bounds

Posted 26 March 2010 - 02:08 AM

The error is here:

public int countNeighbors(int x, int y) {
    int count = 0;

    boolean bb = false;
    try {
        bb = inBounds(x, y);
        if (bb)
            if (grid[x - 1][y].equals("*")) {
            count++;
        }
    } catch (Exception e) {
        e.printStackTrace();
    }

    if ((inBounds(x, y)) && grid[x][y - 1].equals("*")) {
        count++;
    }
...



The fault:

displayName=this]	Life	#163	life.Life@148bd3	
displayName=x]		int	0	0	
displayName=y]		int	1	1	
displayName=count]	int	0	0	
displayName=bb]		boolean	true	true	
displayName=e]		ArrayIndexOutOfBoundsException	#164	
java.lang.ArrayIndexOutOfBoundsException: -1	



inBounds(x, y) is returning true. x has a value of zero and is being used to index grid thusly: grid[x - 1]. clearly out of bounds.

I think the problem is here:

public boolean inBounds(int x, int y) {
    if (x > 0 && x <= 20) {
        return true;
    }
    if (y > 0 && y <= 20) {
        return true;
    }
    return false;
}



inBounds only checks one index at a time.

This post has been edited by n8wxs: 26 March 2010 - 02:13 AM

Was This Post Helpful? 0
  • +
  • -

#8 n8wxs  Icon User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 972
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: Array Out of Bounds

Posted 26 March 2010 - 03:01 AM

Using:

public boolean inBounds(int x, int y) {
    boolean xx = false;
    boolean yy = false;

    if (x > 0 && x < 19) {
        xx = true;
    }
    if (y > 0 && y < 19) {
        yy = true;
    }

    if (xx && yy) {
        return true;
    }

    return false;
}



and initializing grid2:

public void nextGeneration() {
    grid2 = new String[20][20];

// copy of blankGrid()
    for (int i = 0; i < 20; i++) {
        for (int j = 0; j < 20; j++) {
            grid2[i][j] = " ";
        }
    }

    for (int x = 0; x < 20; x++) {
        for (int y = 0; y < 20; y++) {
            if (lives(x, y) && inBounds(x, y)) {
                grid2[x][y] = "*";
            }
        }
    }
    grid = grid2;
}



and adding a print statement:

public static void main(String[] args) throws IOException {
    Life game = new Life();
    Scanner infile = new Scanner(new BufferedReader(new FileReader("life100.txt")));
    game.getFile(infile);
    for (int i = 1; i < 5; i++) {
        game.nextGeneration();
        System.out.println("=============================================");
        game.printGrid();
    }
}



produces this output:

run:
  *   **  **    *   
*   *  *            
      *     *  *   *
*   *         * **  
*       *   * **    
* * *  *     *   *  
           *  **  * 
    *     *  **     
      *    ** *  * *
      * *    **     
   *      *        *
      ** **      *  
* *         *       
**     **     **    
       * *  **  * * 
* *   * * **    **  
  **              * 
  *    *   * *     *
*    **    ****     
   ** * *  *    **  
=============================================
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
=============================================
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
=============================================
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
=============================================
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
BUILD SUCCESSFUL (total time: 0 seconds)


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1