简体   繁体   中英

Out of Bounds Exception on a 2D Ragged Array in Java

Problem solved, I ended up need a seperate counter for the array position. Thanks for the help! I'm writing a small app that takes a string, processes each string into 7-bits of binary code and then fills in a musical scale based on the string. For instance, if I had the binary 1000100, in the key of C Major that would give me the notes C and G(C 0 0 0 G 0 0).

I'm having an issue with a specific piece of code that takes an input of String[] (in which each element is a single character worth of binary, 7-bits) and processes each individual character in the strings themselves and stores the index number of where 1's occur in the string. For example, the string 1000100 would output 1 and 5.

Here's the method that does that:

public static String[][] convertToScale(String[] e){
    String[][] notes = new String[e.length][]; //create array to hold arrays of Strings that represent notes
    for(int i = 0; i < e.length; i++){
        notes[i] = new String[findOccurancesOf(e[i])]; //create arrays to hold array of strings
        for(int x = 0; x < e[i].length(); x++){ 
            if((e[i].charAt(x)) != 48){ //checks to see if the char being evaluated is 0(Ascii code 48)
                notes[i][x] = Integer.toString(x + 1); // if the value isn't 0, it fills in the array for that position.the value at x+1 represents the position of the scale the note is at
            }
        }
    }
    return notes;
}

Here is the code that is uses to get the occurrences of 1 in e[1]:

public static int findOccurancesOf(String s){
    int counter = 0;
    for(int i = 0; i < s.length(); i++ ) {
        if( s.charAt(i) == 1 ) {
            counter++;
        } 
    }
    return counter;
}

The issue I'm having is with the convertToScale method. When using "Hello world" as my input(the input gets converted into 7-bit binary before it gets processed by either of these methods) it passes through the 2nd for-each loop just fine the first time around, but after it tries to fill another spot in the array, it throws

java.lang.ArrayIndexOutOfBoundsException: 3

EDIT:It occurs in the line notes[i][x] = Integer.toString(x + 1); of the convertToScale method. I've run the debugger multiple times through after trying the proposes changes below and I still get the same error at the same line. The findOccurancesOf method returns the right value(When evaluating H(1001000) it returns 2.) So the thing that confuses me is that the out of bounds exception comes up right when it fills the 2nd spot in the array.

Also, feel free to tell me if anything else is crazy or my syntax is bad. Thanks!

In findOccurancesOf() :

if( s.charAt(i) == 1 ) { should be if( s.charAt(i) == '1' ) { to check for the character '1'.

Otherwise it's looking for the character with ASCII value 1.

There is an out of bounds exception because if findOccuranceOf() returns the wrong value, then notes[i] is not constructed with the correct length in the following line of convertToScale() :

notes[i] = new String[findOccurancesOf(e[i])];

In addition, you probably want to use something like:

notes[i][c++] = Integer.toString(x + 1);

with some counter c initialized to 0, if I understand your intentions correctly.

The reason for AIOOBE lies in this line:

notes[i] = new String[findOccurancesOf(e[i])]; //create arrays to hold array of strings

Where you call findOccurancesOf method to find occurance of 1 in your String say Hello which you dont find and return 0 and then you call notes[i][x] = Integer.toString(x + 1); with x as 0. Now since you never allocated space, you get array index out of bound exception.

I would suggest the folowing:

  • Validate your string before assigning the index say to be greater than 0 or something.
  • Initialize you notes[i] as notes[i] = new String[e[i].length];
  • Checking character with single quotes like a == '1' rather than a == 1

The exception is caused by what almas mentioned, note however, that your logical error is most likely inside findOccurencesOf method, if the idea was to find all the '1' chars inside a string you must change to what I outlined below, note the apostrohes. Otherwise a char is getting converted to a byte ascii code, and unless matched with a code of ascii code one, the method will return 0, causing your exception

 if( s.charAt(i) == '1' ) {

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM