is a single character string a number as well?

Stellaferox

Active Member
Licensed User
Hi.

I want to know whether a single character string is also a number, e.g. can it be compared to a number, or is there an EVAL function to convert it to a number? I am getting FormatException errors.......

thnx

Marc
 

specci48

Well-Known Member
Licensed User
Longtime User
Hi Stellaferox,

what are you trying to do?
If needed, basic4ppc uses automatically type conversion, so
B4X:
a = "3"
b = 4
if a > b then
is possible.

Additionally you can convert a single char into ascii value with ASC("r") or an ascii code into its character Chr(58).

If you provide the code causing the exception we'll have a look at it!


specci48
 

Stellaferox

Active Member
Licensed User
Hi specci48,

thnx for your answer. the code is really long so I don't want to bother you with that. Do you know what the errorcode FormatException really means?
Marc
 

Cableguy

Expert
Licensed User
Longtime User
Hi,

If you are sure of the variable beeing compatible, usually format exception error are caused by missing or , or ;...
 

RandomCoder

Well-Known Member
Licensed User
Longtime User
I frequently experience the format exception error when I call up properties of controls with the wrong arguments, particularly when using the Control command to reference to a control.
You should be given a step number and from there it shouldn't be too difficult to determine what is wrong.

Can you just post the section of code that errors?

Regards,
RandomCoder
 

Stellaferox

Active Member
Licensed User
OK, guys, here is the code. It is a program for logistic regression taken (with permission from the author) at http://www.statpages.org/logistic.html (source code). Disregard the stringsigns ($). I know they are not necessary but I am translating from an older basic for PSION code which I made a few years ago.....
just run the program and it errors with a formatexception.
I assume the error in is this part:
IF ChkbxGrouped.Checked = TRUE then
'for zeros
d = SubString(v,0,StrIndexOf(v,",",0)) 'what is value left of comma for zeros
IF d > MaxX then MaxX = d 'set MaxX
IF d < MinX then MinX = d 'set MinX
Y0 (i) = d
sY0 = sY0+d
v = SubString(v,StrIndexOf(v,",",0)+1,1) 'reset dd$, right of comma
d = SubString(v,0,StrIndexOf(v,",",0)) 'what is value left of comma for zeros
'for ones
'what is value left for ones
Y1(i) = d
sY1 = sY1+d
ELSE
IF IsNumber(v) then
Select v
Case 0
Y0(i) = 1
sY0 = sY0+1
Case 1
Y1(i) = 1
sY1 = sY1+1
End Select
END IF
END IF
sC = sC+(Y0(i)+Y1(i))
END IF '// end check if string <> empty


thnx for the trouble......
Marc
 

Attachments

  • LogReg.sbp
    24.6 KB · Views: 188

dzt

Active Member
Licensed User
Hi,

I took a quick look at your code.

You can,t make arithmetic operations without numbers.
xsd(j), xM(j), sC does not contain numbers (empty) when the first error occurs, at line 156.

Try to give them initial values and watch them as they change (MsgBox-ing them is a way, or put their values in a textbox)

Check all your source. I thing there are more errors like this one.

regards
 

Stellaferox

Active Member
Licensed User
I think the problem lies in

Par(0) = LN(sY1/sY0)

a few lines above that sY0 doesnt take the value I want and remains zero thus dividing by zero...
then I get an error in the line

vv = vv + Par(j) * X(i*(nR+1)+j)

saying "input string was not in correct format. I wonder why sY0 doesnt take values because that should be the case with the initial data.
 

Stellaferox

Active Member
Licensed User
OK, I tracked down the error but I don't know why it occurs. It happens right after the IF CheckGrouped = 0 then line.
It goes past this check (because CheckGrouped = 0) and then jumps the IF v = 0 line. Now this is strange because with a msgbox statement right above this IF THEN construction you can check that V = 0 indeed! Then the sY0 doesnt get a value and divison by zero occurs further on.
Now why isn't that line of code executed when v = 0??

IF CheckGrouped = 0 then
IF v=0 then
Y0(i) = 1
sY0 = sY0+1
ELSE
Y1(i) = 1
sY1 = sY1+1
END I
ELSE ....
 

Stellaferox

Active Member
Licensed User
I found that the problem disappears if I use the stement IF ASC(v) = 48 instead of IF v = 0. (and corrected (IF v <=15) to (IF v < -15))
Now that isn't suppose to happen, right?
 

Stellaferox

Active Member
Licensed User
Erel,

Thanks a million man! I couldn't figure out the difference between zero and "0". Everybody else, also many thanks of course....... This was a braincracker.
Marc
 

Stellaferox

Active Member
Licensed User
Yes I got that one instaneously, because it really puzzled me... again, to anyone: zeros are not always what they appear to be...:sign0188:
thnx
Marc
 

derez

Expert
Licensed User
Longtime User
text to number

Hi
regarding your original question - an easy way to make sure that a text variable is changed to a number variable is to perform a math operation :
a = a+0

this is useful when reading a number from a textbox.
David
 
Top