0

I cannot find my exception although the errors are at

at puzzle.Puzzle.isSafe(Puzzle.java:97)
    at puzzle.Puzzle.main(Puzzle.java:49)
Java Result: 1

I need to print a 15x 15 grid with random words in 6 directions and starts in empty spaces.

My java code is as follows.

/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package puzzle;

//import java.util.Arrays;

import java.util.Random;

/**
*
* @author sony
*/
public class Puzzle {
static char[][] grid;

//class solve= new Puzzle();



/**
 * @param args the command line arguments
 */
public static void main(String[] args) {
     grid =new char[15][15];
    String[] words={"hello","coward","heartbeat","beautiful","kind"};
    String[] direction = {"horizontal","horizontalBack","vertical","varticalUp",
                 "diagonal","diagonalBack"};

int i,j,x,y,dir;
Random randGen = new Random();
    for(i=0; i<15 ;i++)
    {
        for(j=0;j<15;j++)
        grid[i][j]='*';
    }


    for(i=0;i< words.length;i++)
    {
        int set=0;
        while(set!=1)
        {
            x =  randGen.nextInt(15);
            y =  randGen.nextInt(15);
            dir = randGen.nextInt(6);
            if((isSafe(x,y,words[i],direction[dir])))
            {
                place(x,y,words[i],direction[dir]);
                set=1;
            }

        }
    }

    for(i=0; i<15; i++)
     {
      for(j=0;j<15;j++)
          System.out.print(grid[i][j]);
      System.out.println("");
     }


} 
   static boolean isSafe(int x,int y, String word, String d)
   {  int len=word.length();
   int i,k,j;
       if(d.equals("horizontal"))
           for(i=y,k=0;i< (y+len);i++,k++)
           {
               if((grid[x][i]!='*')&& (grid[x][i]!=word.charAt(k)) && ((y+len) >15) )
                   return false;
           }
       if(d.equals("horizontalBack"))
           for(i=y,k=0;i >(y-len);i--,k++)
           {
               if((grid[x][i]!='*')&& (grid[x][i]!=word.charAt(k)) && ((y-len) <0)  )
                   return false;
           }
       if(d.equals("vertical"))
           for(i=x,k=0;i <(x+len);i++,k++)
           {
               if((grid[i][y]!='*')&& (grid[i][y]!=word.charAt(k)) && ((x+len) >15)  )
                   return false;
           }
         if(d.equals("verticalUp"))
           for(i=x,k=0;i >(x+len);i++,k++)
           {
               if((grid[i][y]!='*')&& (grid[i][y]!=word.charAt(k)) && ((x-len) <0)  )
                   return false;
           }
         if(d.equals("diagonal"))
         {   k=0;i=y;j=x;
             while((i< (y+len)) && (j< x+len))               {
               if((grid[i][j]!='*')&& (grid[i][j]!=word.charAt(k)) && ((x+len) >15) &&       ((y+len)>15)   )
               {return false;}
               i++;j++;k++;
           }

         }
         if(d.equals("diagonalBack"))
         {   k=0;i=y;j=x;
             while((i> (y-len)) && (j>x-len))               {
               if((grid[i][j]!='*')&& (grid[i][j]!=word.charAt(k)) && ((x-len)<0) && ((y-len)<0)   )
               {return false;}
               i--;j--;k++;
           }

         }


      return true;       


   }

 static void place(int x, int y, String word, String d)
   {  int len = word.length();
   int i,k,j;
       if(d.equals("horizontal"))
           for( i=y, k=0;i< (y+len);i++,k++)
           {
               grid[x][i]=word.charAt(k);
           }

   if(d.equals("horizontalBack"))
           for( i=y,k=0;i> (y-len);i--,k++)
           {
               grid[x][i]=word.charAt(k);
           }
    if(d.equals("vertical"))
           for( i=x,k=0;i< (x+len);i++,k++)
           {
               grid[i][y]=word.charAt(k);
           }

   if(d.equals("verticalUp"))
           for( i=x,k=0;i> (x-len);i--,k++)
           {
               grid[i][y]=word.charAt(k);
           }
   if(d.equals("diagonal"))
   { i=y;j=x;k=0;
           while((i< (y+len)) && (j< (x+len))) 
           {
               grid[i][j]=word.charAt(k);
               i++;j++;k++;

           }

   }
   if(d.equals("diagonalUp"))
   { i=y;j=x;k=0;
           while((i> (y-len)) && (j> (x-len))) 
           {
               grid[i][j]=word.charAt(k);
               i--;j--;k++;

           }

   }




   }

}
1
  • 4
    What did you learn by stepping through this with a debugger? Commented May 29, 2014 at 5:35

4 Answers 4

2

Instead of telling you how to fix your code, I'll help you understand the error.

Log

at puzzle.Puzzle.isSafe(Puzzle.java:97)
    at puzzle.Puzzle.main(Puzzle.java:49)
Java Result: 1

Puzzle.java:97

if (grid[i][j]!='*' && grid[i][j]!=word.charAt(k) && (x+len)>15 && (y+len)>15)

Exception

Exception java.lang.ArrayIndexOutOfBoundsException means that either:

  • i >= grid.length

  • j >= grid[i].length

Detail

grid.length and grid[i].length are determined when the array is created:

grid = new char[15][15];

grid.length is 15

grid[i].length is 15

Moreover

Also, there is the string word:

for (i=y, k=0; i < (y+len); i++, k++)
    word.charAt(k);

This will cause an issue too, when k at some point becomes k >= word.length() so that loop should be like this:

for (i=y, k=0; i < (y+len) && k < word.length(); i++, k++)
    word.charAt(k);
Sign up to request clarification or add additional context in comments.

2 Comments

Thankq so much.. it helped alot!
There is also some error in the code as the desired output isnt got. I am not getting all the words at the output although the 'place' function should place all the words otherwise it should never come out of the loop, yet only 1 or two words are placed in the grid.
0

I can see two bugs.

(1) The line that reads

for(i=x,k=0;i >(x+len);i++,k++)

should read

for(i=x,k=0;i >(x-len);i--,k++)

I think it's line 89.

(2) The bug that's causing this exception is the fact that every time you've got a whole lot of conditions separated by &&, the first one should be && and the subsequent ones should be ||.

For example,

if((grid[x][i]!='*')&& (grid[x][i]!=word.charAt(k)) && ((y+len) >15)) {

should read

if((grid[x][i]!='*')&& (grid[x][i]!=word.charAt(k)) || ((y+len) >15)) {

because you need to return that this square is an unsafe place to start the word if y + len > 15 regardless of what is found at each of the grid squares that you're checking. Similarly, with all the other conditions that look like this.

Comments

0

Please note the typo. varticalUp in the array declaration and verticalUp in the if condition. Although this is not the cause of the problem but its worth noting it.

Comments

-1

I just quickly went through your code and it seems that the problem is in your variable len. See the statement int len=word.length(); on line 68. In fact it should be int len=word.length()-1; because the array index starts from 0. I have not tested it, but you can give it a try.

1 Comment

I haven't downvoted this (I might later), but I don't think it's right. Have a close look at how len is being used.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.