01jayss
01jayss

Reputation: 1449

Java Array Index out of bounds Exception Sort

I currently have the following code. I get ArrayIndexOutofBoundsException at this line.

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 4
at Module3_1_Sort.sort(Module3_1_Sort.java:70)
at Module3_1.s_2d_string(Module3_1.java:155)

The corresponding lines are as follows.

dta[flagcounter] = dta[x];
sortValues = s.sort(sortValues,counter, sortBy, searchterm);

I am pretty sure that it does not exceed the length of the Array...

Can anybody help? Thanks!

code of program follows

    static public void s_2d_string () {
    c.println("2D String Array Program");
    int counter,x;
    c.print("How many entries do you wish to sort? ");
    counter = c.readInt();

    String[][] sortValues = new String[counter+1][2];

    for (x=0;x<counter;x++) {
        c.print("Enter book name: ");
        sortValues[x][0] = c.readLine();
        c.print("Enter book author: ");
        sortValues[x][1] = c.readLine();

    }
    c.print("Which column would you like to sort by? 1 or 2? ");
    int sortBy = c.readInt();
    sortBy = sortBy-1;

    c.print("Enter search term: ");
    String searchterm = c.readLine();

    sortValues = s.sort(sortValues,counter, sortBy, searchterm);

    int flagcounter_int = Integer.parseInt(sortValues[0][0]);

    c.println(flagcounter_int + " results found.");

    for (x=0;x<flagcounter_int;x++) {
        c.println(sortValues[x+1][0] + ", " + sortValues[x+1][1]);
    }
}


static public String[][] sort (String dta[][], int totalNo, int sortBy, String searchterm)  {
    boolean found = false;    
    int flagcounter = 0;
        for (int x=0; x<dta.length;x++) {
            if (sortBy == 0) {
                if (searchterm.equalsIgnoreCase(dta[x][0])) {
                    found = true;
                    flagcounter = flagcounter+1;
                    dta[flagcounter] = dta[x];
                }
            }
            if (sortBy == 1) {
                if (searchterm.equalsIgnoreCase(dta[x][1])) {
                    found = true;
                    flagcounter = flagcounter+1;
                    dta[flagcounter] = dta[x];
                }
            }
        }
    String flagcounter_string = Integer.toString(flagcounter);
    dta[0][0] = flagcounter_string;
    return (dta);
}   

Upvotes: 1

Views: 1444

Answers (2)

Vojislav Stojkovic
Vojislav Stojkovic

Reputation: 8153

Look at the for loop and the way you handle flagcounter inside it. The for loop says:

for (int x = 0; x < dta.length; x++)

and the flagcounter is incremented when you find the search term, before it's used as an index:

flagcounter = flagcounter + 1;
dta[flagcounter] = dta[x];

What happens is that if you have a match in the first row, you keep finding it in the next row (because you overwrite it) and you end up going out of bounds.

Let's look at an example. Say you have this as an input:

Book            Author
-----------------------------
Proven Guilty   Jim Butcher
Naked Sun       Isaac Asimov

So dta will be like this:

[
    ["Proven Guilty", "Jim Butcher"],
    ["Naked Sun", "Isaac Asimov"],
    [null, null]
]

Say you're looking for author "Jim Butcher". So when you enter the loop, you have x = 0, flagcounter = 0. You immediately find the match and what happens is:

flagcounter = flagcounter + 1; // flagcounter is now 1
dta[flagcounter] = dta[x]; // i.e. dta[1] = dta[0];

So now the dta array looks like this:

[
    ["Proven Guilty", "Jim Butcher"],
    ["Proven Guilty", "Jim Butcher"],
    [null, null]
]

You can see what happens: you keep assigning the previous row to the next row and at last you're in the situation where x = 2 and flagcounter = 2 and you try to do dta[3] = dta[2], which goes out of bounds of dta.

As Sabbath suggested, you need to correct the for loop. However, I think you're probably missing something more to make it do what you want. Among other things, there's the fact that dta[flagcounter] = dta[x]; does not assign values in the x row to the flagcounter row, but actually makes the flagcounter row point to x row by reference.

Upvotes: 2

Sabbath
Sabbath

Reputation: 91

Change your code from

for (int x=0; x<dta.length;x++) {
        if (sortBy == 0) {
            if (searchterm.equalsIgnoreCase(dta[x][0])) {
                found = true;
                flagcounter = flagcounter+1;
                dta[flagcounter] = dta[x];
            }
        }
        if (sortBy == 1) {
            if (searchterm.equalsIgnoreCase(dta[x][1])) {
                found = true;
                flagcounter = flagcounter+1;
                dta[flagcounter] = dta[x];
            }
        }
    }

to

for (int x=0; x<totalNo;x++) {
        if (sortBy == 0) {
            if (searchterm.equalsIgnoreCase(dta[x][0])) {
                found = true;
                flagcounter = flagcounter+1;
                dta[flagcounter] = dta[x];
            }
        }
        if (sortBy == 1) {
            if (searchterm.equalsIgnoreCase(dta[x][1])) {
                found = true;
                flagcounter = flagcounter+1;
                dta[flagcounter] = dta[x];
            }
        }
    }

Upvotes: 2

Related Questions