我有一个字符串,我需要将其分解为多个块并将每个块保存在一个数组中以进行操作。我不知道块的长度,但我知道在第 8 个逗号之后开始了新记录。我能够将其分解为正确的块并将其存储在StringBuffer
循环中,但不确定如何将其添加到数组中?
下面的代码显示了我到目前为止所做的事情:
public static void main(String[] args) {
//I would like to store each of these lines in an array
String teachMod = ("1,TSM,501,SM1056,Simon Thorne,TCO200,24,30,"+
"2,TSM,500,AC1157,Peter Jones,TCO104,24,60,"+
"3,TSM,500,SM0156,Simon Thorne,TCO113,12,60,"+
"4,TSM,500,AC1157,Peter Jones,TCO104,24,30,"+
"5,TSM,500,AC1156,Richard Adlam,TCO113,12,60,"+
"6,TSM,500,AC1157,Peter Jones,TCO109,12,90,"+
"7,TSM,503,SM0156,Simon Thorne,CIS414,12,30,"+
"8,TSM,501,AC1156,Richard Adlam,TCO222,12,40,"+
"9,TSM,500,AC1156,Richard Adlam,TCO114,12,60,"+
"10,TSM,501,AC1157,Peter Jones,TCO200,60,40,"+
"11,TSM,500,AC1156,Richard Adlam,TCO106,24,60,"+
"12,TSM,501,SM0156,Simon Thorne,TCO207,12,30,"+
"13,TSM,500,AC1157,Peter Jones,TCO112,24,30,");
StringBuffer sb = new StringBuffer();
char[] c = teachMod.toCharArray();
int j = 0;
for(int i = 0; i < teachMod.length(); i++){
sb.append(c[i]);
if(c[i]==','){
j++;
if(j==8){
//At this point the variable sb holds what I want in an array
System.out.println(sb);
j=0;
sb = new StringBuffer();
}
}
}
}
}//End class