1

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);
}   
6
  • 3
    It would really help debug if you could highlight the line numbers where the Exception is raised. Commented Jul 11, 2012 at 1:35
  • The lines that have problems are located on the top... (the lines that follow CORRESPONDING LINES FOLLOW) Commented Jul 11, 2012 at 1:38
  • 1
    Looking at your code, I don't see anything immediately wrong. I suggest to add a println(flagcounter + " " + x) right before the lines in question, or better yet, use the debugger. This will help you figure out what's going wrong. Commented Jul 11, 2012 at 1:44
  • 2
    Yes. There a bug in your code. We get it. Now please use a debugger (and not just "print debug" statements) to find out what the unexpected values are and why. Commented Jul 11, 2012 at 1:52
  • 1
    You created a array with length of the number you input plus 1 . And you want to use the dta[0][0] to store found information . So you should loop totalNo times but not dta.length times . Try the code in my answer . Commented Jul 11, 2012 at 1:53

2 Answers 2

2

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.

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

Comments

2

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];
            }
        }
    }

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.