为何代码持续抛出java.lang.StringIndexOutOfBoundsException异常?
问题分析与解决:按'|'分割字符串数组时的越界异常问题
我写了一个方法,本意是把字符串数组playlist1d按每个|分割后存入二维数组playlist2d,但一直抛出异常。异常出现在第一个if语句下的temp = playlist1d[counter].charAt(characterCount);行。我尝试在while循环里加col<=5的条件,想让col超过5就停止循环,但它还是会跑到6触发错误。改成col<5能正常运行,但col<6或col<=5都没用。
原数组代码
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和songCount均为数组行数13,目标二维数组playlist2d为[13][6]结构。
出现异常的方法代码
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; }
问题根源
- 循环条件判断滞后:while循环的条件判断是在每次循环开始时,但当处理到第6个
|时,col会先被递增到6,之后才会触发下一次循环的条件检查。而在col变为6之后,代码仍会执行charAt(characterCount),如果此时characterCount已经到达字符串末尾,就会抛出索引越界异常。 - 数据不兼容:数组第11行
"11 | qer | asdaf | tsth | sf | "只有5个有效字段,缺少Rating列数据,但原逻辑仍按6列处理,导致读取到字符串末尾时触发越界。 - 手动解析易出错:逐字符解析的方式对索引控制要求极高,稍有不慎就会出现逻辑漏洞,不如Java内置的字符串分割方法可靠。
解决方案
方案1:使用内置split方法(推荐)
直接用String.split()分割每行字符串,代码简洁且不易出错:
public String[][] splitPlaylist(int songCount, String[] playlist1d) throws IOException { String[][] playlist2d = new String[lineCount][6]; for (int counter = 0; counter < lineCount; counter++) { // 按" | "分割,处理分隔符前后的空格 String[] parts = playlist1d[counter].split(" \\| "); for (int col = 0; col < 6; col++) { // 字段不足时填充空字符串,避免null playlist2d[counter][col] = col < parts.length ? parts[col].trim() : ""; } } return playlist2d; }
方案2:修复原手动解析逻辑
如果坚持使用逐字符解析,需调整索引控制逻辑,避免col越界:
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++){ rowLength = playlist1d[counter].length(); characterCount = 0; col = 0; nullCount = 0; while(characterCount < rowLength && col <= 5){ // 先判断索引合法性,再读取字符 if(characterCount >= rowLength) break; temp = playlist1d[counter].charAt(characterCount); tempString = String.valueOf(temp); if(tempString.equals(tempLine)){ characterCount++; // 仅当col未到5时才递增,避免超过数组列数 if(col < 5){ col++; nullCount = 0; } else { // 已填满6列,直接跳出循环 break; } } else { if(nullCount == 0){ playlist2d[counter][col] = String.valueOf(temp); } else { playlist2d[counter][col] += temp; } characterCount++; nullCount++; } } // 填充缺失的列为空字符串 for(; col < 6; col++){ if(playlist2d[counter][col] == null){ playlist2d[counter][col] = ""; } } } return playlist2d; }
内容的提问来源于stack exchange,提问作者Michael
相关产品推荐
相关产品推荐

