1

I am having trouble solving the minesweepers problem because I am getting an index out of bound and can't figure out why: Here is the link of the problem

I think my code isn't exactly efficient but I just want to solve this , here is the code :

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

public class Mine_Sweepers {

    public static void main(String [] args) throws IOException
    {
       BufferedReader bf=new BufferedReader(new InputStreamReader(System.in));

      //Taking the dimensions of the grid...

      StringTokenizer s=new StringTokenizer(bf.readLine());
      int b=Integer.parseInt(s.nextToken()), a=Integer.parseInt(s.nextToken());
      while(b!=0 && a!=0)
      { 
            mine(b,a);
            s=new StringTokenizer(bf.readLine());
            b=Integer.parseInt(s.nextToken());   
            a=Integer.parseInt(s.nextToken());
      }
   }
      public static void mine(int b,int a) throws IOException
      {

            BufferedReader bf=new BufferedReader(new InputStreamReader(System.in));
            boolean array_first=false,array_last=false,string_first=false,string_last=false;

            String []x=new String[a];

            for(int i=0;i<a;i++)
                x[i]=bf.readLine();
            for(int i=0;i<a;i++)
            {
               //Here I am checking whether this is the first field in the array or if it is the last
                array_first=(i==0)?true:false;
                array_last=(i==a-1)?true:false;
                for(int j=0; j<b; j++)
                {
                   //Here I am checking whether this is the first char in the String in the array or if it is the last
                   string_first=(j==0)?true:false;
                   string_last=(j==a-1)?true:false;
                   int counter=0; 
                   if(x[i].charAt(j)!='*')
                   {

                      if(!string_first)
                      {
                         if(x[i].charAt(j-1)=='*')
                            counter++;
                            if(!array_first & x[i-1].charAt(j-1)=='*')
                                counter++;
                                if(!array_last & x[i+1].charAt(j-1)=='*')
                                   counter++;

                      }
                      if(!string_last )
                      {
                            if(x[i].charAt(j+1)=='*')
                                counter++;
                                if(!array_first && x[i-1].charAt(j+1)=='*')
                                    counter++;
                                    if(!array_last & x[i+1].charAt(j+1)=='*')
                                    counter++;
                     }
                     if(!array_first & x[i-1].charAt(j)=='*')
                        counter++;

                        if(!array_last & x[i+1].charAt(j)=='*')
                            counter++;

                            System.out.print(counter);
                     }
                     else
                        System.out.print('*');
                }
                System.out.println();
            }
        }

    }
  • Do you have `b < a` or something like that? If not, in `string_last=(j==a-1)?true:false;` what if `j == a + 1` for example? At what line do you get the error? – IVlad Aug 10 '15 at 13:24
  • Ohh that is a bug it should be : (j==b-1),. Thanks for pointing it out. – Omar Hossam Ahmed Aug 10 '15 at 13:28

1 Answers1

1

You are using &, which does not shortcircuit.

if(!array_first & x[i-1].charAt(j+1)=='*')

So both !array_first and x[i-1].charAt(j+1)=='*' are evaluated, no matter what. You want the second part evaluated only if the first one evaluates to true, so if !array_first is false (so array_first is true), the rest does not get evaluated (because if it does, i-1 will be negative, and you will get an error). Use && for this. Not just on this line, but everywhere.

If you use patterns like:

if <valid indexes> and <access to those indexes>:
    do work

You should always use && as your logical and operator.

Community
  • 1
  • 1
IVlad
  • 43,099
  • 13
  • 111
  • 179
  • I mixed up and thought the '&' are the one to shortcircuit. Thank you very much! It works perfectly now. – Omar Hossam Ahmed Aug 10 '15 at 13:45
  • @OmarHossamAhmed if an answer helped solve your problem, please consider marking it as accepted by clicking on the tick mark to the left of it. If you want, you can also upvote the answer by clicking on the up arrow (`^`) in the same location. – IVlad Aug 10 '15 at 13:47