943,929 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 1220
  • Java RSS
Aug 24th, 2008
0

array out of bounds help

Expand Post »
Im just trying to figure out why I have a array out of bounds that all can somebody help?
java Syntax (Toggle Plain Text)
  1. public class Life2 {
  2.  
  3. public static void main(String[] args) throws Exception{
  4. boolean dead;
  5. int winsX = 0;
  6. int num = 1;
  7.  
  8. int count = 0;
  9. int countL;
  10. int countD;
  11. char [][] array1 = {
  12. {' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '},
  13. {' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '},
  14. {' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '},
  15. {' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '},
  16. {' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '},
  17. {' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '},
  18. {' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '},
  19. {' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '},
  20. {' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '},
  21. {' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '},
  22. {' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '},
  23. {' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '}
  24. };
  25.  
  26. char [][] array2 = {
  27. {' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '},
  28. {' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '},
  29. {' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '},
  30. {' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '},
  31. {' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '},
  32. {' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '},
  33. {' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '},
  34. {' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '},
  35. {' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '},
  36. {' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '},
  37. {' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '},
  38. {' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '}
  39.  
  40. };
  41. /* Fill array with 0's for dead cells */
  42. for(int i = 0; i < array1.length; i++){
  43. for(int j = 0; j < array1.length; j++){
  44. array1[i][j] = ' ';
  45. }
  46. }
  47. /*Fills in array 2 with 0's */
  48. for(int a = 0; a < array1.length; a++){
  49. for(int b = 0; b < array1.length; b++){
  50. array1[a][b] = ' ';
  51. }
  52. }
  53.  
  54.  
  55. String Num = javax.swing.JOptionPane.showInputDialog("How many cells are alive");
  56. int pow = Integer.parseInt(Num);
  57. for(int y = 0; y < pow; y++){
  58. String RowX = javax.swing.JOptionPane.showInputDialog("Enter cell row " + (y+1) + ":");
  59. int rowX = Integer.parseInt(RowX);
  60. String ColumnX = javax.swing.JOptionPane.showInputDialog("Enter cell column" + (y+1) + ":");
  61. int columnX = Integer.parseInt(ColumnX);
  62.  
  63. array1[(rowX)][(columnX)] = 'X';
  64.  
  65.  
  66. for(int x = 0; x < array1.length; x++){
  67. for(int z = 0; z < array1.length; z++){
  68. System.out.print(array1[x][z]);
  69. if(z < 11)
  70. System.out.print("|");
  71. }
  72. System.out.println();
  73. if(x < 11)
  74. System.out.println("___________________");
  75. }
  76. //break;
  77. }
  78. // 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] = ' ';
  79. // 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] = ' ';
  80. // 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] = ' ';
  81. // 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] = ' ';
  82. // 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] = ' ';
  83. // 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] = ' ';
  84. // 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] = ' ';
  85. // 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] = ' ';
  86. // 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] = ' ';
  87. // 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] = ' ';
  88.  
  89.  
  90. // String Choice = javax.swing.JOptionPane.showInputDialog("Would you like to continue the next gen?");
  91. // num = Integer.parseInt(Choice);
  92. num = 1;
  93. do{
  94. int neighbors;
  95. for (int i = 0; i < 11; i++)
  96. {
  97. for (int j = 0; j < 11; j++)
  98. {
  99. neighbors = 0;
  100. //Begin counting number of neighbors:
  101. if (array1[i-1][j-1] == 'X') neighbors += 1;
  102. if (array1[i-1][j] == 'X') neighbors += 1;
  103. if (array1[i-1][j+1] == 'X') neighbors += 1;
  104. if (array1[i][j-1] == 'X') neighbors += 1;
  105. if (array1[i][j+1] == 'X') neighbors += 1;
  106. if (array1[i+1][j-1] == 'X') neighbors += 1;
  107. if (array1[i+1][j] == 'X') neighbors += 1;
  108. if (array1[i+1][j+1] == 'X') neighbors += 1;
  109.  
  110. //Apply rules to the cell:
  111. if (array1[i][j] == 'X' && neighbors < 2)
  112. array1[i][j] = ' ';
  113. else if (array1[i][j] == 'X' && neighbors > 3)
  114. array1[i][j] = ' ';
  115. else if (array1[i][j] == 'X' && (neighbors == 2 || neighbors == 3))
  116. array1[i][j] = 'X';
  117. else if (array1[i][j] == ' ' && neighbors == 3)
  118. array1[i][j] = 'X';
  119. }
  120. }
  121. for(int i = 1; i < 1; i++){
  122. for(int j = 1; j < 11; j++){
  123. array1[i][j] = array2[i][j];
  124. }
  125. }
  126.  
  127. for(int i = 1; i < 1; i++){
  128. for(int j = 1; j < 1; j++){
  129. array2[i][j] = ' ';
  130. }
  131. }
  132. /* Print new Generation */
  133. for(int x = 0; x < 11; x++){
  134. for(int z = 0; z < 11; z++){
  135. System.out.print(array2[x][z]);
  136. if(z < 11)
  137. System.out.print("|");
  138. }
  139. System.out.println();
  140. if(x < 11)
  141. System.out.println("___________________");
  142. }
  143.  
  144. String Diablo = javax.swing.JOptionPane.showInputDialog("If you want to continue Press 1 if not press 0");
  145. int diablo = Integer.parseInt(Diablo);
  146.  
  147.  
  148.  
  149.  
  150. }while(num == 1);
  151. }
  152.  
  153. }
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
Dio1080 is offline Offline
47 posts
since Aug 2007
Aug 25th, 2008
0

Re: array out of bounds help

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--

java Syntax (Toggle Plain Text)
  1.  
  2. public class InitializeArrayTest{
  3.  
  4. public static void main(String... args){
  5. char[][] dCharArray = new char[12][]; // creating a double array with 12 array of unknown length
  6.  
  7. for(int i = 0; i < 12; i++){
  8. dCharArray[i] = new char[12]; // allocating arrays of length 12 to the arrays of the double array
  9. }
  10.  
  11. for(int i = 0; i < dCharArray.length; i++){ // the length (or amount) of arrays stored in dCharArray
  12. for(int j = 0; j < dCharArray[i].length; j++){ // length of the array at location dCharArray[i]
  13. System.out.print(""+dCharArray[i][j] + ','); // prints the value to the screen as a character
  14. }
  15. System.out.println(); // enters a new line
  16. }
  17. }
  18. }

--also I can see a potential problem in this chunk of code--

java Syntax (Toggle Plain Text)
  1. //...
  2. if (array1[i-1][j-1] == 'X') neighbors += 1; // what happens when i and j are 0? -1,-1 is out of bounds!
  3. if (array1[i-1][j] == 'X') neighbors += 1; // i = 0, -1, 0, out of bounds!
  4. if (array1[i-1][j+1] == 'X') neighbors += 1; // i = 0 or j = 11, out of bounds!
  5. if (array1[i][j-1] == 'X') neighbors += 1; // etc
  6. if (array1[i][j+1] == 'X') neighbors += 1; // etc
  7. if (array1[i+1][j-1] == 'X') neighbors += 1; // etc
  8. if (array1[i+1][j] == 'X') neighbors += 1; // etc
  9. if (array1[i+1][j+1] == 'X') neighbors += 1; // etc..
  10. //...

--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 =)
Reputation Points: 392
Solved Threads: 108
Posting Shark
Alex Edwards is offline Offline
971 posts
since Jun 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: hibernate
Next Thread in Java Forum Timeline: Array probleam





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC