Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8205621
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T08:19:10+00:00 2026-06-07T08:19:10+00:00

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

  • 0

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);
}   
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T08:19:14+00:00Added an answer on June 7, 2026 at 8:19 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I currently have the following code: public MyObject SessionStore { get { if (Session[MyData]
I currently have the following code to get a specific value i need from
I currently have the following code for java. public class lesson8 { static Console
I currently have the following code. I am trying to get the second, third,
I currently have the following code which delays showing a fixed bar after a
Currently I have the following code to flash a DataGrid cell when it's value
I currently have the following js code function clearMulti(option) { var i; var select
The following code shows what I currently have. It is an adapter for circular
I have the following code currently: <DataTemplate DataType={x:Type vm:SectionViewModel}> <ScrollViewer> <ItemsControl ItemsSource={Binding ViewModels}> </ItemsControl>
I am currently working on Problem 62 I have tried the following code to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.