0

I am working on solution for the CCC 2018 Robo Thieves Problem, it a simplified version of the original problem. My problem is that it will give me this "Index 5 out of bounds for length 5" when I executed my code and I'm not sure why it is happening. Half of my program executes and then this error occurs.

import java.util.*;

public class RoboThieves {
    public static void main(String args[]) {
        Scanner sc = new Scanner(System.in);
        ArrayList<Integer> rowPos = new ArrayList<Integer>();
        ArrayList<Integer> colPos = new ArrayList<Integer>();
        int sRow = -1; // robot pos
        int sCol = -1;
        int dotCounter = 0;
        int stepCounter = 0;

        int rowSize = sc.nextInt();
        int colSize = sc.nextInt();
        char[][] factoryGrid = new char[rowSize][colSize];

        for (int i = 0; i < rowSize; i++) {

            String rowChars = sc.next().toUpperCase();

            for (int j = 0; j < colSize; j++) {

                factoryGrid[i][j] = rowChars.charAt(j);
            }

        }

        // check to see if the grid was inputted properly (with square brackets)
        /*
         * for (char [] row: factoryGrid) { System.out.println(Arrays.toString(row)); }
         */

        // check to see if the grid was inputted properly (as inputted)
        for (int i = 0; i < rowSize; i++) {
            for (int j = 0; j < colSize; j++) {

                System.out.print(factoryGrid[i][j]);
            }
            System.out.println();
        }

        // locate dots and store their row and col in arraylists
        for (int i = 0; i < rowSize; i++) {

            for (int j = 0; j < colSize; j++) {
                if (factoryGrid[i][j] == '.') {
                    rowPos.add(i);
                    colPos.add(j);
                    dotCounter++;
                }
            }

        }

        // print dot location to check
        for (int i = 0; i < rowPos.size(); i++) {
            System.out.println("Dot Position = " + "(" + rowPos.get(i) + "," + colPos.get(i) + ")");
        }

        // locate robot position
        for (int i = 0; i < rowSize; i++) {

            for (int j = 0; j < colSize; j++) {
                if (factoryGrid[i][j] == 'S')
                    sRow = i;
                    sCol = j;
            }

        }

        // print camera location to check
        System.out.println("Camera Position = " + "(" + sRow + "," + sCol + ")");

        //System.out.println(dotCounter); // test to see if counter works

        char above = getAbove(factoryGrid, sRow, sCol);
        char right = getRight(factoryGrid, sRow, sCol);
        char below = getBelow(factoryGrid, sRow, sCol);
        char left = getLeft(factoryGrid, sRow, sCol);

        if (above == '.') {

            boolean canMove = check360(factoryGrid, sRow, sCol);
            // check if camera is around dot
            if (canMove == true) {
                // set robot position to dot position and old position to W
                sRow = sRow - 1;
                // sCol = sCol;

                factoryGrid[sRow][sCol] = 'W';

                dotCounter--;
                stepCounter++;

            } else {
                // this is if there is a camera in the 360 radius of the open space
                System.out.println("You cannot move to the space beside because there is a camera in your sightline!");
            }

        } else if (right == '.') {

            boolean canMove = check360(factoryGrid, sRow, sCol);
            // check if camera is around dot
            if (canMove == true) {
                // set robot position to dot position and old position to W
                // sRow = sRow;
                sCol = sCol + 1;

                factoryGrid[sRow][sCol] = 'W';

                dotCounter--;
                stepCounter++;

            } else {
                // this is if there is a camera in the 360 radius of the open space
                System.out.println("You cannot move to the space beside because there is a camera in your sightline!");
            }

        } else if (below == '.') {

            boolean canMove = check360(factoryGrid, sRow, sCol);
            // check if camera is around dot
            if (canMove == true) {
                // set robot position to dot position and old position to W
                sRow = sRow + 1;
                // sCol = sCol;

                factoryGrid[sRow][sCol] = 'W';

                dotCounter--;
                stepCounter++;

            } else {
                // this is if there is a camera in the 360 radius of the open space
                System.out.println("You cannot move to the space beside because there is a camera in your sightline!");
            }

        } else if (left == '.') {
            boolean canMove = check360(factoryGrid, sRow, sCol);
            // check if camera is around dot
            if (canMove == true) {
                // set robot position to dot position and old position to W
                // sRow = sRow;
                sCol = sCol - 1;

                factoryGrid[sRow][sCol] = 'W';

                dotCounter--;
                stepCounter++;

            } else {
                // this is if there is a camera in the 360 radius of the open space
                System.out.println("You cannot move to the space beside because there is a camera in your sightline!");
            }
        } else {

            System.out.println(
                    "The robot cannot move to any spaces try inputting a factory layout that can produce an answer.");

        } // end if above dot (yes)

        System.out.println(stepCounter);

        for (int i = 0; i < rowSize; i++) {
            for (int j = 0; j < colSize; j++) {

                System.out.print(factoryGrid[i][j]);
            }
            System.out.println();
        }

    } // end main method

    public static char getLeft(char[][] factGrid, int cRow, int cCol) {
        return factGrid[cRow][(cCol - 1)];

    }

    public static char getAbove(char[][] factGrid, int cRow, int cCol) {
        return factGrid[(cRow - 1)][(cCol)];

    }

    public static char getBelow(char[][] factGrid, int cRow, int cCol) {
        return factGrid[cRow + 1][cCol];

    }

    public static char getRight(char[][] factGrid, int cRow, int cCol) {
        return factGrid[cRow][(cCol + 1)];

    }

    public static boolean check360(char[][] factGrid, int cRow, int cCol) {
        boolean canMove = true;

        char left = getLeft(factGrid, cRow, cCol);
        char above = getAbove(factGrid, cRow, cCol);
        char right = getRight(factGrid, cRow, cCol);
        char below = getBelow(factGrid, cRow, cCol);

        if (left == 'C' || above == 'C' || right == 'C' || below == 'C') {
            canMove = false;
        }
        return canMove;
    }

} // end main program

Mateo Lini
  • 7
  • 1
  • 3
  • 1
    Without trying to debug your code, it should also tell you what line is failing. That's a big clue. Index 5 *is* out of range for a list of length 5 (index goes from 0 to 4). Look at the line being reported and figure out either why you're going too far -or- why you don't have enough items in your list. – Joseph Larson Oct 31 '19 at 20:10

1 Answers1

0

Upon first look, I expect that the issue may lie in your getLeft(), getAbove(), getRight(), and getBelow() methods.

In these methods, you give it a value for cRow and cCol and add or subtract 1. However, you need to make sure that the index you are querying does not exceed the size of the double array factGrid, or go below 0.

For example, in your getRight() method, you might try:

public static char getRight(char[][] factGrid, int cRow, int cCol) {
    if(factGrid[0].length > (cCol + 1)) {
        return factGrid[cRow][(cCol + 1)];
    } else {
        return '';
    }
}
btw15
  • 106
  • 5
  • is this correct, or it is like `(cCol + 1) > factGrid[0].length` - which would possible end in an exception, better use `<` (sorry, I prefer this *order* since it reads more natural IMO) – user85421 Oct 31 '19 at 20:19
  • The order doesn't really matter, as long as there is the appropriate sign. I did realize I had the wrong sign before (<). In the valid case, the length of the array should be larger than the cCol + 1, otherwise it would result in an error. – btw15 Oct 31 '19 at 20:26
  • I know it doesn't matter to the compiler/machine - but to the human order can matter (*In My Opinion* it is easier to read, more natural - I do not want to know if "the length is greater than something", but I want to know if "the index is something" - see [Is Code For Computers or for People](https://stackoverflow.com/a/522907/85421) - still also a question of personal preferences, again, just my point of view, certainly you may prefer that way - also valid) – user85421 Oct 31 '19 at 20:30