Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

CROSSWORD checking array index out of bounds exception

807588May 1 2009 — edited May 1 2009
Hello all i am designing a crossword puzzle which is a 12x12 String[][] and when i put a word into the puzzle i want to make sure that it is not touching any other words. in my if statement i test every direction around the word. But lets say the word starting position is at [0][0] when i check up and left to make sure there is an empty space i get an array index out of bounds is there a way to catch this somehow inside an if statement?
//down
					if(dir == 0 && x+w.getWordLength() <= 12 /*&& board[x-1][y].equals("o") && board[x][y-1].equals("o") && board[x][y+1].equals("o")*/)
					{
						dcount=dcount+1;
						for(int i=0;i < w.getWordLength();i++)
						{
							board[x+i][y].setStr(""+w.getWord().charAt(i));
							board[x+i][y].setUsed(true);
							w.setPosition(board[x][y].getPosition());
						}
						System.out.println(dcount+" DOWN: @position "+w.getPosition()+", "+w.getDescription());	
					}
					//across
					if(dir == 1 && y+w.getWordLength() <= 12 /*&& board[x][y-1].equals("o") && board[x-1][y].equals("o") && board[x+1][y].equals("o")*/)
					{
						acount = acount +1;
						for(int i=0;i < w.getWordLength();i++)
						{
							board[x][y+i].setStr(""+w.getWord().charAt(i));
							board[x][y+i].setUsed(true);
							w.setPosition(board[x][y].getPosition());	
						}
						System.out.println(acount+" ACROSS: @position "+w.getPosition()+", "+w.getDescription());		
					}
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on May 29 2009
Added on May 1 2009
5 comments
195 views