Jump to content
Search In
  • More options...
Find results that contain...
Find results in...
Sign in to follow this  
faftek

Can someone help me debug this code?

Do you know that line of code is causing the exception or event the array that is causing the out of bounds error? It looks like there is a lot of stuff going on and just a quick read, I can't really tell what is going on. Little tip if you don't know where the exception is going on, a decent debug method is called probing. All it is, is putting println statements in your code to see how far the code makes it before breaking or even printing out array values to make sure that they are correct.

I actually figured out my error was due to a logic bug :S the X and Y were supposed to be switched. fixed that and switched one of the test method and all works now :D

Recommended Posts

Posted · Original PosterOP

I'm currently working on a program, and I've got this 95%+ done just for some reason when there are randomly generated 2D arrays an index out of bounds exception is returned. If there is no index out of bounds error then this code works perfectly. I am appreciative to any advice and/or help I recieve and thanks in advance :) (sorry if it's kinda shit as of now I tend to make the code work then go back and make it pretty)

 

**forgot to mention in the header this is in java

    public static int[][] answer(int[][] population, int x, int y, int strength) {        Values = population;        currentX = x;        currentY = y;        testX = new ArrayList();        testY = new ArrayList();        //instantiate spotsDone        for (int j = 0; j < Values.length; j++) {            spotsDone = new boolean[Values.length][Values[j].length];        }                //set initial values for spotsDone        for (int i = 0; i < spotsDone.length; i++) {            for (int j = 0; j < spotsDone[i].length; j++) {                spotsDone[i][j] = false;            }        }        //check if first value matches if yes begin testing        if (strength >= Values[x][y]) {            Values[x][y] = -1;            test(strength, x, y);        }        return Values;    }    public static boolean test(int strength, int x, int y) {        //test first scenarios        testLeft(strength);        currentX = x;        currentY = y;        testRight(strength);        currentX = x;        currentY = y;        testUp(strength);        currentX = x;        currentY = y;        testDown(strength);                //tests every place marked to be tested        while (!testX.isEmpty()) {            currentX = (int) testX.get(0);            currentY = (int) testY.get(0);            testLeft(strength);            currentX = (int) testX.get(0);            currentY = (int) testY.get(0);            testRight(strength);            currentX = (int) testX.get(0);            currentY = (int) testY.get(0);            testUp(strength);            currentX = (int) testX.get(0);            currentY = (int) testY.get(0);            testDown(strength);            testX.remove(0);            testY.remove(0);        }        return false;    }    //tests if spots above in grid    public static void testUp(int strength) {        if (currentX > 0) {            currentX -= 1;        }        if (spotsDone[currentX][currentY]) {            return;        }        spotsDone[currentX][currentY] = true;        if (Values[currentX][currentY] <= strength) {            Values[currentX][currentY] = -1;            testX.add(currentX);            testY.add(currentY);        }    }    //tests spots below in grid    public static void testDown(int strength) {        if (currentX < Values.length - 1) {            currentX += 1;        }        if (spotsDone[currentX][currentY]) {            return;        }        spotsDone[currentX][currentY] = true;        if (Values[currentX][currentY] <= strength) {            Values[currentX][currentY] = -1;            testX.add(currentX);            testY.add(currentY);        }    }    //tests spots left in grid    public static void testLeft(int strength) {        if (currentY > 0) {            currentY -= 1;        }        if (spotsDone[currentX][currentY]) {            return;        }        spotsDone[currentX][currentY] = true;        if (Values[currentX][currentY] <= strength) {            Values[currentX][currentY] = -1;            testX.add(currentX);            testY.add(currentY);        }    }    //tests spots right in grid    public static void testRight(int strength) {        if (Values.length != 1) {            if (currentY > 0) {                if (currentY < Values[currentY + 1].length - 1 && currentY < Values[currentY].length - 1 && currentY < Values[currentY - 1].length - 1) {                    currentY += 1;                }            } else {                if (currentY < Values[currentY + 1].length - 1 && currentY < Values[currentY].length - 1) {                    currentY += 1;                }            }        }        if (spotsDone[currentX][currentY]) {            return;        }        spotsDone[currentX][currentY] = true;        if (Values[currentX][currentY] <= strength) {            Values[currentX][currentY] = -1;            testX.add(currentX);            testY.add(currentY);        }    }
Link to post
Share on other sites

Do you know that line of code is causing the exception or event the array that is causing the out of bounds error? It looks like there is a lot of stuff going on and just a quick read, I can't really tell what is going on. Little tip if you don't know where the exception is going on, a decent debug method is called probing. All it is, is putting println statements in your code to see how far the code makes it before breaking or even printing out array values to make sure that they are correct.

Link to post
Share on other sites
Posted · Original PosterOP · Best Answer

Do you know that line of code is causing the exception or event the array that is causing the out of bounds error? It looks like there is a lot of stuff going on and just a quick read, I can't really tell what is going on. Little tip if you don't know where the exception is going on, a decent debug method is called probing. All it is, is putting println statements in your code to see how far the code makes it before breaking or even printing out array values to make sure that they are correct.

I actually figured out my error was due to a logic bug :S the X and Y were supposed to be switched. fixed that and switched one of the test method and all works now :D

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  


×