| | |
array out of bounds help
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
![]() |
Im just trying to figure out why I have a array out of bounds that all can somebody help?
java Syntax (Toggle Plain Text)
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); } }
A few questions...
First, why do you declare two arrays and define them full of spaces then iterate them through loops to assign space characters to them when they already have that value?
You can reduce code-bloat by simply instantiating your arrays to have 12x12 characters.
Furthermore the initial value of a character should be the space character, so there's no need to allocate a static built-in type to each indice of the double array.
An example--
--also I can see a potential problem in this chunk of code--
--This reminds me of the Doodlebugs project our teacher made us do. We needed a way for bugs to move in random locations (to the left, right north or south) and had to be able to determine if the move was within the bounds of the board. Same deal with the checkers program we had to create.
You may want to check for neighbors of a particular element, you may want to instead use objects as X's instead of char values. Then you can increment the amount of neighbors that particular object has and iterate through the list of objects and display the amount of neighbors they have.
Also, can you elaborate what it is you are trying to do with this program/game? i'm fairly interested to know =)
First, why do you declare two arrays and define them full of spaces then iterate them through loops to assign space characters to them when they already have that value?
You can reduce code-bloat by simply instantiating your arrays to have 12x12 characters.
Furthermore the initial value of a character should be the space character, so there's no need to allocate a static built-in type to each indice of the double array.
An example--
java Syntax (Toggle Plain Text)
public class InitializeArrayTest{ public static void main(String... args){ char[][] dCharArray = new char[12][]; // creating a double array with 12 array of unknown length for(int i = 0; i < 12; i++){ dCharArray[i] = new char[12]; // allocating arrays of length 12 to the arrays of the double array } for(int i = 0; i < dCharArray.length; i++){ // the length (or amount) of arrays stored in dCharArray for(int j = 0; j < dCharArray[i].length; j++){ // length of the array at location dCharArray[i] System.out.print(""+dCharArray[i][j] + ','); // prints the value to the screen as a character } System.out.println(); // enters a new line } } }
--also I can see a potential problem in this chunk of code--
java Syntax (Toggle Plain Text)
//... if (array1[i-1][j-1] == 'X') neighbors += 1; // what happens when i and j are 0? -1,-1 is out of bounds! if (array1[i-1][j] == 'X') neighbors += 1; // i = 0, -1, 0, out of bounds! if (array1[i-1][j+1] == 'X') neighbors += 1; // i = 0 or j = 11, out of bounds! if (array1[i][j-1] == 'X') neighbors += 1; // etc if (array1[i][j+1] == 'X') neighbors += 1; // etc if (array1[i+1][j-1] == 'X') neighbors += 1; // etc if (array1[i+1][j] == 'X') neighbors += 1; // etc if (array1[i+1][j+1] == 'X') neighbors += 1; // etc.. //...
--This reminds me of the Doodlebugs project our teacher made us do. We needed a way for bugs to move in random locations (to the left, right north or south) and had to be able to determine if the move was within the bounds of the board. Same deal with the checkers program we had to create.
You may want to check for neighbors of a particular element, you may want to instead use objects as X's instead of char values. Then you can increment the amount of neighbors that particular object has and iterate through the list of objects and display the amount of neighbors they have.
Also, can you elaborate what it is you are trying to do with this program/game? i'm fairly interested to know =)
![]() |
Similar Threads
- delete array and assigns array to pointer (C++)
- Return Array from C++ (C++)
- Help with array bounds in C (C)
- array and arrayList (Java)
- multidimensional array (C)
- Run-time Error when printing Array Contents. (C)
- how to set up my for loop to print my array in reverse (Java)
- help clear mess in single-scripted array (C++)
- Array with names (C)
Other Threads in the Java Forum
- Previous Thread: hibernate
- Next Thread: Array probleam
| Thread Tools | Search this Thread |
2dgraphics android api apple applet application arguments array arrays automation banking binary binarytree bluetooth capture chat chatprogramusingobjects class classes client code color component count database derby design eclipse eclipsedevelopment encryption error event exception fractal game givemetehcodez graphics gridlayout gui html ide if_statement image input integer interface j2me java javadesktopapplications javaprojects jlabel jni jpanel julia keyword linux list loop macintosh map method methods midlethttpconnection mobile netbeans newbie object os print printing problem producer program programming project projectideas read recursion reference replaysolutions ria scanner screen server set size sms sort sourcelabs sql stop string swing threads transforms tree ui unicode validation windows





