array out of bounds help plz

Page 1 of 1

1 Replies - 445 Views - Last Post: 24 August 2008 - 06:44 PM Rate Topic: -----

#1 Dio1080  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 28
  • Joined: 25-March 08

array out of bounds help plz

Posted 24 August 2008 - 06:20 PM

Just trying to figure out why i keep getting this array out of bounds error after I run the program, can anybody help?

public class Life2 {

public static void main(String[] args) throws Exception{
boolean dead;
int winsX = 0;
int num = 1;

int count = 0;
int countL;
int countD;
char [][] array1 = {
					{' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '},
					{' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '},
					{' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '},
					{' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '},
					{' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '},
					{' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '},
					{' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '},
					{' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '},
					{' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '},
					{' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '},
					{' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '},
					{' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '}
				};
				
char [][] array2 = {
					{' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '},
					{' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '},
					{' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '},
					{' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '},
					{' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '},
					{' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '},
					{' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '},
					{' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '},
					{' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '},
					{' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '},
					{' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '},
					{' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '}
					
				};
	/* Fill array with 0's for dead cells */
		for(int i = 0; i < array1.length; i++){
			for(int j = 0; j < array1.length; j++){
				array1[i][j] = ' ';
			}
		}
		/*Fills in array 2 with 0's */
		for(int a = 0; a < array1.length; a++){
			for(int b = 0; b < array1.length; b++){
				array1[a][b] = ' ';
			}
		}


      String Num = javax.swing.JOptionPane.showInputDialog("How many cells are alive");
		int pow = Integer.parseInt(Num);
		for(int y = 0; y < pow; y++){
			String RowX = javax.swing.JOptionPane.showInputDialog("Enter cell row " + (y+1) + ":");
         int rowX = Integer.parseInt(RowX);
			String ColumnX = javax.swing.JOptionPane.showInputDialog("Enter cell column" + (y+1) + ":");
			int columnX = Integer.parseInt(ColumnX);
	
				array1[(rowX)][(columnX)] = 'X';
			

	for(int x = 0; x < array1.length; x++){
				for(int z = 0; z < array1.length; z++){
					System.out.print(array1[x][z]);
					if(z < 11)
						System.out.print("|");
					}
					System.out.println();
					if(x < 11)
						System.out.println("___________________");
			}
			//break;
	                }
      // array[0][0] = ' '; array[0][1] = ' '; array[0][2] = ' '; array[0][3] = ' '; array[0][4] = ' '; array[0][5] = ' '; array[0][6] = ' '; array[0][7] = ' '; array[0][8] = ' '; array[0][9] = ' ';
// 		array[1][0] = ' '; array[1][1] = ' '; array[1][2] = ' '; array[1][3] = ' '; array[1][4] = ' '; array[1][5] = ' '; array[1][6] = ' '; array[1][7] = ' '; array[1][8] = ' '; array[1][9] = ' ';
// 		array[2][0] = ' '; array[2][1] = ' '; array[2][2] = ' '; array[2][3] = ' '; array[2][4] = ' '; array[2][5] = ' '; array[2][6] = ' '; array[2][7] = ' '; array[2][8] = ' '; array[2][9] = ' ';
// 		array[3][0] = ' '; array[3][1] = ' '; array[3][2] = ' '; array[3][3] = ' '; array[3][4] = ' '; array[3][5] = ' '; array[3][6] = ' '; array[3][7] = ' '; array[3][8] = ' '; array[3][9] = ' ';
// 		array[4][0] = ' '; array[4][1] = ' '; array[4][2] = ' '; array[4][3] = ' '; array[4][4] = ' '; array[4][5] = ' '; array[4][6] = ' '; array[4][7] = ' '; array[4][8] = ' '; array[4][9] = ' ';
// 		array[5][0] = ' '; array[5][1] = ' '; array[5][2] = ' '; array[5][3] = ' '; array[5][4] = ' '; array[5][5] = ' '; array[5][6] = ' '; array[5][7] = ' '; array[5][8] = ' '; array[5][9] = ' ';
// 		array[6][0] = ' '; array[6][1] = ' '; array[6][2] = ' '; array[6][3] = ' '; array[6][4] = ' '; array[6][5] = ' '; array[6][6] = ' '; array[6][7] = ' '; array[6][8] = ' '; array[6][9] = ' ';
// 		array[7][0] = ' '; array[7][1] = ' '; array[7][2] = ' '; array[7][3] = ' '; array[7][4] = ' '; array[7][5] = ' '; array[7][6] = ' '; array[7][7] = ' '; array[7][8] = ' '; array[7][9] = ' ';
// 		array[8][0] = ' '; array[8][1] = ' '; array[8][2] = ' '; array[8][3] = ' '; array[8][4] = ' '; array[8][5] = ' '; array[8][6] = ' '; array[8][7] = ' '; array[8][8] = ' '; array[8][9] = ' ';
// 		array[9][0] = ' '; array[9][1] = ' '; array[9][2] = ' '; array[9][3] = ' '; array[9][4] = ' '; array[9][5] = ' '; array[9][6] = ' '; array[9][7] = ' '; array[9][8] = ' '; array[9][9] = ' ';
	

    	// String Choice = javax.swing.JOptionPane.showInputDialog("Would you like to continue the next gen?");
// 		num = Integer.parseInt(Choice);
   num = 1;
		do{
		int neighbors;
     for (int i = 0; i < 11; i++)
     {
         for (int j = 0; j < 11; j++)
         {
             neighbors = 0;
             //Begin counting number of neighbors:
             if (array1[i-1][j-1] == 'X') neighbors += 1;
             if (array1[i-1][j] == 'X') neighbors += 1;
             if (array1[i-1][j+1] == 'X') neighbors += 1;
             if (array1[i][j-1] == 'X') neighbors += 1;
             if (array1[i][j+1] == 'X') neighbors += 1;
             if (array1[i+1][j-1] == 'X') neighbors += 1;
             if (array1[i+1][j] == 'X') neighbors += 1;
             if (array1[i+1][j+1] == 'X') neighbors += 1;
             
             //Apply rules to the cell:
             if (array1[i][j] == 'X' && neighbors < 2)
                array1[i][j] = ' ';
             else if (array1[i][j] == 'X' && neighbors > 3)
                array1[i][j] = ' ';
             else if (array1[i][j] == 'X' && (neighbors == 2 || neighbors == 3))
                array1[i][j] = 'X';
             else if (array1[i][j] == ' ' && neighbors == 3)
                array1[i][j] = 'X';
         }
     }
for(int i = 1; i < 1; i++){
			for(int j = 1; j < 11; j++){
				array1[i][j] = array2[i][j];
			}
		}
		
		for(int i = 1; i < 1; i++){
			for(int j = 1; j < 1; j++){
					array2[i][j] = ' ';
			}
		}
/* Print new Generation */
for(int x = 0; x < 11; x++){
				for(int z = 0; z < 11; z++){
					System.out.print(array2[x][z]);
					if(z < 11)
						System.out.print("|");
					}
					System.out.println();
					if(x < 11)
						System.out.println("___________________");
			}

    String Diablo = javax.swing.JOptionPane.showInputDialog("If you want to continue Press 1 if not press 0");
	 int diablo = Integer.parseInt(Diablo);
		

		
		
	   }while(num == 1);
		 }
		
}



Is This A Good Question/Topic? 0
  • +

Replies To: array out of bounds help plz

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8329
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: array out of bounds help plz

Posted 24 August 2008 - 06:44 PM

Here

			String RowX = javax.swing.JOptionPane.showInputDialog("Enter cell row " + (y+1) + ":");  
			int rowX = Integer.parseInt(RowX);  
			String ColumnX = javax.swing.JOptionPane.showInputDialog("Enter cell column" + (y+1) + ":");  
			int columnX = Integer.parseInt(ColumnX);  

			array1[(rowX)][(columnX)] = 'X';  


You never check that rowX and columnX do not exceed your original array size which is [11][11] or something like that

You need something like

[code]
if(rowX >= array1.length)
sorry too large
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1