I am still getting my grip on Java. I need some help in looping through an array.
My array looks like this;
String [] allRecords = ["[BEGIN RECORD]", "[ID]1", "[cName]Agnes", "[Age]12", "[END RECORD]", "[BEGIN RECORD]", "[ID]2", "[cName]Hellen", "[Age]5", "[END RECORD]", "[BEGIN RECORD]", "[ID]3", "[cName]Jack", "[Age]34", "[END RECORD]" ];
//i use the below code to identify the beginning and end of a record in the array
String beginRecord = "[BEGIN RECORD]";
boolean foundBeginRecord = false;
int foundIndex = 0;
for (int i=0; i<allRecords.length; i++) {
if (beginRecord.equals(allRecords[i])) {
foundBeginRecord = true;
foundIndex = i+1; //added one
break;
}
}
String endRecord = "[END RECORD]";
boolean foundEndRecord = false;
int foundEnd = 0;
for (int i=0; i<allRecords.length; i++) {
if (endRecord.equals(allRecords[i])) {
foundEndRecord = true;
foundEnd = i; //one NOT added
break;
}
}
//i then use the below code to slice off part of the array
String [] partAllRecords = Arrays.copyOfRange(allRecords, foundIndex, foundEnd);
//this gives me a new sub-array like this: "[ID]1", "[cName]Agnes", "[Age]12"
The above code works OK. What I need now is to read/slice another portion from the allRecords array i.e.; "[ID]2", "[cName]Hellen", "[Age]5" and then slice the next block "[ID]3", "[cName]Jack", "[Age]34" till the end of the allRecords Array.
How can I do this?
Thank you!