Android Question help with for loop

apty

Active Member
Licensed User
Longtime User
I have a for loop for reading a file as list. The file content looks like below:

General family
private family
other family

Myname
myhouse
myfamily

Othername
otherhouse
otherfamily

From the above list, i want to pick only
Myname
Myhouse
Myfamily

Since the file will keep changing, Myname and the two below it will not always appear at the same place. They might even appear after Othefamily. So i would like to always search for Myname and then pick the next two entries below it. Something like in the below code. Please help

B4X:
Dim famlist As List
    famlist=File.ReadList(File.DirAssets,"familymain.txt")
    For i=0 To famlist.Size-1
        Dim family As String
        family=famlist.Get(i)
    If family.Contains("Myname") Then
' Here i want to pick the next two entries after Myname
        
    End If
    Next
 

Mahares

Expert
Licensed User
Longtime User
' Here i want to pick the next two entries after Myname

How about something simple like this:
B4X:
Dim famlist, MyList2 As List
    MyList2.Initialize
    famlist=File.ReadList(File.DirAssets,"familymain.txt")
    For i=0 To famlist.Size-1
        Dim family As String
        family=famlist.Get(i)
        If family.Contains("Myname") Then
            ' Here i want to pick the next two entries after Myname
            MyList2.add(famlist.get(i))
            MyList2.add(famlist.get(i+1))
            MyList2.add(famlist.get(i+2))
            Exit
        End If
    Next
    For i=0 To MyList2.Size-1
        Log(MyList2.Get(i))  'displays: Myname, myhouse,myfamily
    Next
 
Upvote 0
Top