b4ppc code vs b4a code

jschuchert

Active Member
Licensed User
Longtime User
I have been at this for 3 hours now and must get to bed but I am sure someone will see what I have done wrong. I am doing as much porting of my b4ppc code as possible into b4a but this one really has me stumped. the routine finds the coordinates of points entered in a textbox, then cross multiplies them to find an area. The ppc code works beautifully. Please review code below:
code from b4ppc
B4X:
Sub btnAreaOK_Click
points=txtareapoints.Text 
pts()=StrSplit(points,"-")    [color=red]this is b4ppc code[/color]
x=ArrayLen(pts())
'Msgbox(x)
lastpoint=pts(x-1)
firstpoint=pts(0)
lastpoint=pts(x-1)
'test for first and last points being the same
If lastpoint <>firstpoint Then '99 Then
Msgbox ("The last point must match first point")
Goto areaend
End If

'It is from here down that is important
j=0 'first point in line of numbers
FileOpen(c,strfilename,cRead)
lineoftext=FileRead(c)
Do Until lineoftext=EOF
coord()=StrSplit(lineoftext,",")
If coord(0)=pts(j) Then  'first point in line of numbers
dblnorth=coord(1)
dbleast=coord(2)
Exit
End If
lineoftext=FileRead(c)
Loop
'FileClose(c)

For i=1 To x-1
'If i>x Then Exit
FileClose(c)
FileOpen(c,strfilename,cRead)
lineoftext=FileRead(c)
Do Until lineoftext=EOF
coord()=StrSplit(lineoftext,",")
skipnextline:
If coord(0)=pts(i) Then
dblnortha=coord(1)
dbleasta=coord(2)
Areasqft = Areasqft + (dblNorth * dbleasta - dblnortha * dblEast)
dblnorth=dblnortha
dbleast=dbleasta
Exit
End If
lineoftext=FileRead(c)
Loop
Next
FileClose(c)

'b4a code
B4X:
Sub btnOK_click
'find # of points used for area calc
Dim x,areasqft,pts(),j,firstpt,points,ptnum
Dim numpts,num(),total1,total2
x=0
points="9,7,3,4,2,8,0,"
num=Regex.Split(",",points)
For i=0 To 20
ptnum=num(i)
x=x+1
If ptnum="0" Then
Exit
End If
Next
numpts=x-1

'here is where the comparison should begin
'get coord of first point in line of numbers
j=0 'first point in line of numbers
Dim cline()
Dim reader As TextReader 
reader.Initialize (File.openinput(File.DirInternal,main.strfilename ))
Dim lineoftext As String
lineoftext=reader.ReadLine ' get the first line
Do While lineoftext<>Null
cline=Regex.Split(",",lineoftext)'added this here
If cline(0)=num(j) Then
main.dblnorth=cline(1)
main.dbleast=cline(2)
Exit
End If
lineoftext=reader.ReadLine ' get next line - this is tested for Null on next loop
Loop
reader.Close

'get remainder of coordinates for points
For i=1 To x-1
reader.Close 
reader.Initialize (File.openinput(File.DirInternal,main.strfilename ))
lineoftext=reader.ReadLine 
Do Until lineoftext=Null  'also tried do while lineoftext<>null
cline=Regex.Split(",",lineoftext)
If cline(0)=num(i) Then
main.dblnortha=cline(1)
main.dbleasta=cline(2)
Areasqft = Areasqft + (main.dblNorth * main.dbleasta) - (main.dblnortha * main.dblEast) [color=red] error is 'numberformat exception [/color]
main.dblnorth=main.dblnortha
main.dbleast=main.dbleasta
End If
lineoftext=reader.ReadLine 
Loop
Next
reader.close
areasqft=areasqft/2 'sets area for all tangents
txtresults.text=txtresults.text & areasqft
End Sub

I just can't see it so hope someone else can. The error is thrown in the line containing 'areasqft=areasqft ..... and says numberformat exception. could it be the typing of the variables? Sorry to be such a pest. Thanks for all of your support.

Jim Schuchert
 

kickaha

Well-Known Member
Licensed User
Longtime User
Jim,

Had a quick look at the code, and as it is at the moment you have a comment wrong:
B4X:
Areasqft = Areasqft + (main.dblNorth * main.dbleasta) - (main.dblnortha * main.dblEast)  error is [COLOR="Red"]'[/COLOR]numberformat exception
should be:
B4X:
Areasqft = Areasqft + (main.dblNorth * main.dbleasta) - (main.dblnortha * main.dblEast) [COLOR="Red"] '[/COLOR] error is numberformat exception
Which throws a numberformat exception. It is not as simple as that is it? It is a self - fulfilling error if it is.
 
Upvote 0

agraham

Expert
Licensed User
Longtime User
It's probably because you have declared, amongst others, areasgft as a String (by default) and are using it as a number before assigning it a value so the empty string it contains causes the error. Unlike Basic4ppc Basic4android does not equate an empty string to zero in a numeric expression.

Try either declaring it as a Number or assigning 0 or "0" to it before using it.
 
Upvote 0

jschuchert

Active Member
Licensed User
Longtime User
Agraham, you hit the nail on the head. As soon as I changed areasqfI to a double, it solved the problem. I didn't get the right answer but I can figure that one out. I keep forgetting that string is the default type. I am used to the 'variant' where the software automatically 'types' it for me according to the context in which it is used. Thanks so much.

And, kickaha, good eye but that 'comment' was only for my post and not in the original code or it would have been picked up during the compiling. Thanks for looking.

Jim Schuchert
 
Upvote 0

agraham

Expert
Licensed User
Longtime User
I am used to the 'variant' where the software automatically 'types' it for me according to the context in which it is used.
Basic4android does coerce types to and from each other as best it can and in most cases you won't have a problem. However as I mentioned Basic4android does not equate an empty string to a zero number so you would need to have initialised the string variable to 0 before using it.

As Basic4android supports types it is far better to get used to declaring variables as the appropriate type for its intended use. Don't forget that it also supports typed returns from Subs as well.
 
Upvote 0
Top