0

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!

1
  • You can add a loop to repeat this action multiple times. Commented Mar 24, 2014 at 18:23

2 Answers 2

1

Your existing code is close and can be modified pretty easily to do what you want. The key thing to remember, which you are not doing now, is to start where you left off, instead of restarting at 0. So you have (greatly simplified for illustration):

int foundIndex = 0;
for (int i=0; i<allRecords.length; i++)
   ... find start record

int foundEnd = 0;
for (int i=0; i<allRecords.length; i++) {
   ... find end record

Note that you start at 0 each time. However, you know a couple of things:

  • The start record won't be before the previous end, so we can start searching just after the previous record.
  • The end record won't be before the start, so we can start searching at the start index.

Then, by saving the location of the end of the previous record, and picking up from there, your logic can now be repeatedly in a loop until all valid records are consumed from the input.

With that in mind, again very over-simplified:

int foundIndex, foundEnd = -1;

do {

    foundIndex = 0;
    for (int i=foundEnd + 1; i<allRecords.length; i++)
       ... find start record

    foundEnd = 0;
    for (int i=foundIndex + 1; i<allRecords.length; i++) {
       ... find end record

} while a record was found;

There are other possible ways to simplify your code (e.g. use an ArrayList with indexOf(), use a simple state machine, etc.), but the above stays pretty close to your current code.

Sign up to request clarification or add additional context in comments.

Comments

0

First, thank you Trenin and Jason for your guidance. I struggled with the task and for the benefit of someone else one day, i will paste below the code that has worked for me;

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]"};

String beginRecord = "[BEGIN RECORD]";
String endRecord = "[END RECORD]";                
int foundIndex = 0;
int foundEnd = 0;

      for (int i=0; i<allRecords.length; i++) {
      if (endRecord.equals(allRecords[i])) {
           foundEnd = i;    
           break;   
           }                
      }

      //by saving the location of the end of the previous record, and picking up from there, your logic can now be repeatedly in a loop until all valid records are consumed from the input
      foundEnd = foundEnd-1; //arrays are zero based


      for (int i=0; i<allRecords.length; i++) {
          if (beginRecord.equals(allRecords[i])) {
             foundIndex = i+1;  //arrays are zero based
             String [] partAllRecords = Arrays.copyOfRange(allRecords, foundIndex, foundIndex+foundEnd);                   
             System.out.println(Arrays.toString(partAllRecords)); 
             //prints below arrays in logcat
             //[[ID]1, [cName]Agnes, [Age]12]
             //[[ID]2, [cName]Hellen, [Age]5]
             //[[ID]3, [cName]Jack, [Age]34]
         }
      }

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.