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:
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 |
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.
Michael is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.