Search code examples
javastringindexoutofbounds

Why does this code keep returning "java.lang.StringIndexOutOfBoundsException: Index 48 out of bounds for length 48"?


This method was supposed to separate an array 'playlist1d' at every '|' and then send them into the 2d array 'playlist2d' but it keeps returning the same error each time.

    public String[][] charCounterRow(int songCount, String[] playlist1d)throws IOException{
        Character temp;
        int characterCount=0, col = 0, rowLength, nullCount=0;
        String playlist2d[][] = new String[lineCount][6];
        String tempLine = "|", tempString;
        for(int counter = 0; counter < lineCount; counter++){
            temp = playlist1d[counter].charAt(characterCount);
            tempString = String.valueOf(temp);
            rowLength = playlist1d[counter].length();
            while(characterCount < rowLength && col <= 5){
                if(tempString.equals(tempLine)){
                    characterCount++;
                    col++;
                    nullCount = 0;
                    temp = playlist1d[counter].charAt(characterCount);
                    tempString = String.valueOf(temp);
                }
                else if(!tempString.equals(tempLine)){
                    if(nullCount == 0){
                        playlist2d[counter][col] = String.valueOf(temp);
                    }
                    else{
                        playlist2d[counter][col] += temp;
                    }
                    characterCount++;
                    temp = playlist1d[counter].charAt(characterCount);
                    tempString = String.valueOf(temp);
                    nullCount++;
                }
            }
            characterCount = 0;
            col=0;
        }
        return playlist2d;
    }

The problem keeps occuring in the line "temp = playlist1d[counter].charAt(characterCount);" under the first 'if' statement. I tried fixing it by adding the 'col<=5' within the while loop but for some reason the code block within it keeps running even though col exceeds 5

Here's the text in the array:

This should be under playlist1d

 private static String[] playlist1d = new String[] {
            "List | Title | Artist | Album | Genre | Rating | ",
            "1 | qwer | asd | zxcv | asdf | 2 | ",
            "2 | wert | sdfg | xcvb | sfdg | 3 | ",
            "3 | erty | dfgh | cvbn | dfgh | 1 | ",
            "4 | qwer | asdf | zxcv | sedfg | 5 | ",
            "5 | wert | sdf | asdf | qwer | 4 | ",
            "6 | poij | wert | asdf | bsfs | 4 | ",
            "7 | test | fdsf | gfddg | oij | 4 | ",
            "8 | tret | gfgd | sdfd | we | 1 | ",
            "9 | t | s | d | f | 4 | ",
            "10 | poij | wert | sdfgshdg | a | 2 | ",
            "11 | qer | asdaf | tsth | sf | ",
            "12 | qwer | asdf | zxcv | asfd | 2 |"
    };

lineCount and songCount would both be just the amount of rows in the array or 13 in this case.

I expected the code under the while loop to just stop after the 'col' variable goes past 5 but for some reason it goes until 6, then the error pops up. I also tried making it col < 5 in the while loop and that works and limits col to 4, but it just wouldn't work for col < 6 or col <=5.


Solution

  • You're trying to access a character at rowLength. You ensure characterCount is less than rowLength, but then increment characterCount++ just before use.

    One solution is to stop looping at rowLength -1, so that characterCount never exceeds your array size.

    Before:

    rowLength = playlist1d[counter].length();
    

    After:

    rowLength = playlist1d[counter].length() - 1;
    

    This way the statement temp = playlist1d[counter].charAt(characterCount); doesn't try to access an element greater than its own size (the classic "index starts at zero" problem).

    Another solution is to move your incrementation until after you use the index, but this may affect other logic.

    Unrelated, but I find myself using String.split() in times like this to avoid tracking each character. It may require more processing power than your solution, but is often easier to read and debug.