0

I was asked to develop a game that involves characters exploring a world. The world is a 2D array. The player starts in some cell in the array and progresses to the next level by reaching any cell on the edge of the array. On each turn, a player can move north, south, east or west. However the player cannot move back to a cell it has already visited. I must use the string contains method for this. My code keep giving me an array out of bounds error string index out of range -1. I have no idea why it is doing this. I would really appreciate some help on this to make it work. Thank you so much. This code is written in java using Netbeans. I need my string contains method to work.

The error happens on this line: String bestdirection = path.substring(0,commaIdx);

Here is the whole code:

 public static void main(String[] args) {
    // TODO code application logic here
    //making array 
 Scanner scanner = new Scanner(System.in);
    System.out.print("How many rows are in the maze? ");
int rows = scanner.nextInt();
    System.out.print("How many colums are in the maze? ");
int colums = scanner.nextInt();
int [][] maze = new int [rows][colums];
for (int i = 0; i< maze.length; i++){
    System.out.print("Enter the danger in row " + (i+1) + ", separated by spaces: ");
    for (int j=0; j<maze[i].length; j++){



    maze[i][j] = scanner.nextInt();


}

}
           System.out.print("Enter the starting x coordinate: ");
    int x = scanner.nextInt();
    System.out.print("Enter the starting y coordinate: ");
    int y = scanner.nextInt();
    System.out.println("Moving to " + x + "," + y + "(danger level " + maze[x][y] + ")");
    String path = (Integer.toString(x) + Integer. toString(y));
    for ( int i=0; i<maze.length; i++){
        for( int j=0; j<maze[i].length; j++){
            if (x == i && y == j){
                System.out.print("*");

            }
            else{ 
                System.out.print(maze[i][j] + " ");
            }


        }
        System.out.println("");
    }

    while (x !=0 && y!=0 && x !=(rows -1) && y != (colums -1)){
    int left = maze[x][y-1];
    int right = maze[x][y+1];
    int up = maze[x-1][y];
    int down = maze[x+1][y];
    int commaIdx = path.indexOf(",");
    String bestdirection = path.substring(0,commaIdx);
   String bestdirection2 = path.substring(commaIdx);

   // if ( x == x && y == y+1){
      // left=1000;
  //  }
    //int count=0;
    if (left < right && left < up && left< down && !path.contains(path)){
        y = y-1;
         System.out.println("Moving to " + x+ "," + y + " (danger level " + maze[x][y] + ")" );
        for (int i =0; i<maze.length; i++ ){
            for (int j=0; j<maze[i].length; j++){

            if (x ==i && y==j){
                System.out.print("*");

            }
            else {
                System.out.print(maze[i][j] + " ");
            }
                System.out.println("");
            }

       // System.out.println("Moving to " + x + y + " (danger level " + maze[x][y] + ")" );
       // count++;

    }
       // if (x ==x && y ==(y-1)){
           // right =1000;
       // }

     if(right < left && right < up && right < down && !path.contains(path)){
        y= y+1;



        }
   // if (x == (x-1) && y==y){
        //up= 1000;
   // }
     if (up < right && up < left && up < down && !path.contains(path)){
        x = x-1;


    } 
    //if (x == (x+1) && y==y){
      //  down = 1000;
   // }
     if (down < right && down < left && down < up && !path.contains(path)){
        x = x+1;


    }
    }
    }


   // int total = maze[x][y] + maze[x][y];

    if (x ==0 || y ==0 || x == (rows -1) || y== (colums -1)){

        for (int i =0; i< maze.length; i++ ){
            for (int j=0; j<maze[i].length; j++){
                if (x ==i && y==j){
                    System.out.println("*");
                }
                else{
                    System.out.println(maze[i][j] + " ");
                }
            }
            System.out.println("");
        }
        System.out.println("Exited the world at: " + x + "," + y + " total danger faced: " );


        }
    }

    }
1
  • 1
    If you want people to try and read your code, indent it properly Commented Oct 27, 2017 at 23:33

2 Answers 2

1

That is because your String variable path does not contain a comma , which will return you an index of -1 in:

int commaIdx = path.indexOf(",");

Thus causing the StringIndexOutOfBoundsException when you run

String bestdirection = path.substring(0,commaIdx);

Check and make sure your commaIdx has a valid value first.

Sign up to request clarification or add additional context in comments.

Comments

1

I think you forgot to add "," when you create path variable. The problem might be solved after you add the comma in the string as below:

String path = (Integer.toString(x) + "," + Integer.toString(y));

Comments

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.