Android Tutorial How to get out of a loop

I have several lines of data like this in a file
1,1000,1000,point1
2,1027.699,1030.228,point2
3,1045.671,1070.345,point3
4,2345.123,1098.346,point4
.
.
etc.

When I enter a point number in the txtFrom.text box and click my OK button, the code should go through the file until it finds that point number and attach the other data (dblnorth,dbleast) to it. Then I want to resume from where I called the sub (getcoord) and use the recovered data in calculations. My problem is that I don't know how to properly leave the sub after getting what I want. I can't use 'Goto or 'exit sub' so I need some method to accomplish this. Here is the code so far:

B4X:
Sub getcoord()
Dim reader As TextReader 
reader.Initialize (File.openinput(File.DirInternal,main.strfilename ))
Dim lineoftext As String
Dim cline()
'get first line
lineoftext=reader.readline
cline=regex.split(",",lineoftext)
if cline(0)=txtFrom.text then
main.dblnorth=cline(1)
main.dbleast=cline(2)
[color=red]found it in first line soi want to leave this sub and go make calculations. In B4ppc I could use a 'Goto" command but not in B4a [/color]
End If
'If the first line does not contain what I want I will go through the rest of the file until I find the correct point number

Do While lineoftext<>Null
lineoftext=reader.ReadLine 
cline=Regex.Split(",",lineoftext)
If cline(0)=txtfrom.Text Then
main.strpointno=cline(0)
main.dblnorth=cline(1)
main.dbleast=cline(2)
[color=red]same story as above. I need to use what I found somewhere else in the app[/color]
End If
Loop
reader.Close 
End Sub

Incidentally, when the loop encounters the end of the data, it throws a null exception error. How do I prevent that? I will be using this type of code frequently during my application.

Any help or reference to help will be greatly appreciated.

Jim Schuchert
 

jschuchert

Active Member
Licensed User
Longtime User
Thank you, Erel and Andy. No, it is I who is missing something. I believe 'return' works also well. I thought you might be out hunting arrows from last weekend. i have been busy and have almost 4 routines out of 29 completed but now finding things don't work as expected. Take a look at this code:
B4X:
Dim reader As TextReader 
reader.Initialize (File.openinput(File.DirInternal,main.strfilename ))
Dim lineoftext As String
Dim cline()
Do While lineoftext<>Null
lineoftext=reader.ReadLine 
cline=Regex.Split(",",lineoftext)
If cline(0)=txtfrom.Text Then
main.dblnorth=cline(1)
main.dbleast=cline(2)
Return
End If
Loop
reader.Close

I originally put several points in my file with another routine. If you review an earlier message today you will see what I mean. I thought all was fine until I tried to proceed from a point created from one of the prior ones. The error message is that there is a nullpointer exception at the "cline=regex.split...." line.Does this mean it can't find the point I started with to create the next one? I put a msgbox just before the write to file command and the proper point was there. Back to the drawing board??

I know it's difficult for anyone to know what I am talking about so here is a short version. I have an 'input' routine where I write points to the file. Let's say I put points 1 through 4 with their data. Now, using the above code, i find point 4 and do some calculations to create point 7 and write it to the file using a different routine. From all indications, it worked fine. no errors. Now I want to start with point 7 by putting it where point 4 was and calculate point 8. That's the problem previously described. I suspect it is somewhere in the above code, like not looking a the first line of the file before rifling through the others points Thanks in advance.

Jim
 

kickaha

Well-Known Member
Licensed User
Longtime User
Jim,

As I read your code, what is happening is that you are testing for Null
B4X:
Do While lineoftext<>Null
THEN you read a line
B4X:
lineoftext=reader.ReadLine
When you get to the end of the file, this line will be Null, thus giving the error.

A better way:
B4X:
Dim reader As TextReader 
reader.Initialize (File.openinput(File.DirInternal,main.strfilename ))
Dim lineoftext As String
Dim cline()
lineoftext=reader.ReadLine ' get the first line
Do While lineoftext<>Null
cline=Regex.Split(",",lineoftext)
If cline(0)=txtfrom.Text Then
main.dblnorth=cline(1)
main.dbleast=cline(2)
Return
End If
lineoftext=reader.ReadLine ' get next line - this is tested for Null on next loop
Loop
reader.Close

Edit to add:

I dont know why it is not picking up the point if you are sure it is there. Try adding a TextBox before the compare to show the values of cline(0) and txtfrom.Text. I suspect you may have a leading or trailing space on one of them.
 
Last edited:

jschuchert

Active Member
Licensed User
Longtime User
:sign0098:Andy, my friend, I think the code you posted solved both of the problems. Apparently, mine wasn't picking up the first line where the last lineoftext was placed in the file. It seems to work fine, now. Thank you so much. Sorry about the crack about your looking for arrows. I'm sure you are a very fine archer. I may have more for you so stand by.

Jim
 

jschuchert

Active Member
Licensed User
Longtime User
Andy,

I believe I have discovered what the problem is. All of a sudden the light came on and I could see it. Since I am using a comma as a (split) separator, when a calculation produces a number greater than 1000, a comma is inserted into the number, like 1,000. Then in my next routine where I need to extract a number, it uses that extra comma and gives me a weird number. Is there a way to suppress that comma in a large number? If so, that should solve my problem. I think I ran into the same thing either with b4ppc or one of the NSBasic programs and there was a solution but I can't remember. I will have to experiment with the fields in the designer. Maybe 'TEXT" is the answer. I'll explore it tomorrow. I haven't fully explored the documentation yet so it may be there, also.

Jim
 
Top